Interface, Inheritance and abstract class

Interface,Inheritance & Abstract Class:-

Inheritance, Interface and abstract class

Inheritance :

The concept of extending a class by another class is known as inheritance.

A class which is inherited is known as Parent class, Base class or Super class.

A class which extends base class is known as Derived class or child class.


Abstract Class:

If a class contains one or more than one abstract method (abstract method is defined by keyword abstract and contains only declaration of that method.) then the class is declared with keyword abstract and known as abstract class.


Interface :

An Interface in java is known as Contract which must be followed by its implementing class (Child class).

If a class contains all methods as abstract method then, the class can be declared as Interface.

Important notes on interface:

  1. only public and abstract modifiers are allowed to use in interface. (Static are not allowed)
  2. If variable is defined inside the interface, then it must be initialized and it is final by default.

To read more on interface, refer this article.


Example 1:

interface IMario
{
  Void behavior();
}

class  Mario implements IMario
{
 public void behavior()
 {
     System.out.println("This is simple Mario");
 }
}

class  SuperMario implements IMario
{
  public void behavior()
  {
     System.out.println("This is Super Mario with power");
  }
}

class  TestMario
{
   public static void main(String args[])
   {
        System.out.println("----game started----");
        Imario obj=new Mario();
        Obj.behavior();
        System.out.println(-----After Mashroom-----");
        obj=new SuperMario();
        Obj.behavior();
        System.out.println(-----After Power Loss-----");
        obj=new Mario();
        Obj.behavior();
   }
}

o/p:-

—–Game Started——

This is simple Mario.

—–After Mashroom——

This is Super Mario with power.

—–After Power Loss—–

This is simple Mario.

Example 2:


interface A
{
   void displayA()
}

abstract class B
{
   public void displayB()
   {
     System.out.println("Display-B");
   }
   abstract public void display();
}

class  c extends B implements A
{
   Public void displayB()
  {
      System.out.println("Abstract Display-B");
  }

  public void displayA()
 {
     System.out.println("Display-A");
  }
}

class  TestClass
{
  public static void main(String args[])
 {
   C obj=new C();
   Obj.display();
   Obj.displayA();
   Obj.displayB();
 }
}

Posted

in

by


Related Posts

Comments

17 responses to “Interface, Inheritance and abstract class”

  1. Thyagaraj Avatar
    Thyagaraj

    Nice, When Explained abstract class with the Super mario Game. Easy to Understand.
    Thanks a lot for this article.

  2. […] To know more about inheritance and abstract classes, refer this article. Sharevar dzone_url = "https://jitendrazaa.com/blog/java/interface/"; var dzone_title = "Interface in JAVA"; If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader. […]

  3. Abhimanyu Avatar
    Abhimanyu

    This is wrong

    1. Sadia Ghani Avatar
      Sadia Ghani

      yes i agree. in main class what is Imario?

  4. harsha Avatar
    harsha

    second pgm is wrong u have to override the abstract method

  5. Java Training In Chennai Avatar

    1. A class can implement more than one interface(Multiple Inheritance is possible with interface). But A class can not extend more than one abstract class(Multiple Inheritance is not possible with class).

    2. If You want to add a new method in interface, you need to implement that method in all the classes that implementing the interface. But if you want to add a new method in abstract class, you can add it as a non abstract method. You don’t need to implement it in all the classes that extending the abstract class.

    3. Interface don’t have constructors. But Abstract class has constructors.

  6. Amey Avatar
    Amey

    Where did you get idea to use Mario..awesome job bro..

    1. JitendraZaa Avatar
      JitendraZaa

      He he he.. Just to make Fun.. I was playing this game a lot when i was child 🙂

  7. JitendraZaa Avatar
    JitendraZaa

    Can you please share your thought, whats wrong here.. I would try to correct

    1. Praveena Avatar
      Praveena

      Nothing is wrong in the 2nd example.But i can see display() method is missing in class c. Can u show some differences between abstraction and inheritance too. I understand the basic defintion. Yeah, inheritance is the ability of one object to acquire the the properties of another. Abstraction is having one or more abstract classes( ofcourse it can have non-abstract class too). I know i cant have abstract methods in the class unless i declare the class with keyword abstract. is that the difference correct??

    2. Venkatesh Varma Avatar
      Venkatesh Varma

      Class c should implement the display() method from class b if you want to use it in class c. I can see that you have used the display() method on class c in the test class. Correct me if i am wrong.

  8. prem Avatar
    prem

    thanks lot it’s really useful to me i got clear idea to abstract and interface

  9. venkat Avatar
    venkat

    the inherited class should want to have a ovverided method for its base class abstact method else it will throw an error

    public class C:B,A
    {
    public new void displayB()
    {
    Console.WriteLine(“Overloaded Display-B”);
    }

    public void displayA()
    {
    Console.WriteLine(“Display-A”);
    }

    public override void diaplay()
    {
    Console.WriteLine(“0verrided display”);
    }
    }

  10. pramod nawale Avatar
    pramod nawale

    Good example…………
    In second example,we need to define method display() for class C, which comes from class B

  11. CompilatiOn Errorr Avatar
    CompilatiOn Errorr

    Should we implement all the methods regardless of abstract or non-abstract methods if we implement abstract class??

  12. Alexander Avatar
    Alexander

    I’m a late-comer to this post. But thank you, Jitendra. Your posts have been invaluable to me throughout my career. I’ve read a few different posts on Inheritance but didn’t *quite* internalize it until I read this post.

Leave a Reply to Java Training In ChennaiCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading