Java don't support "multiple inheritance" i.e. in java it is not possible that a subclass has more than one super classes.
For example in java we can't extends more than one super class for a sub class like this,
"public class subclass extends superclass1,superclass2,superclass3"
Java solve this problem using "INTERFACE". Interface is just like a java class that has variable/variables and method/methods but the only different is that interface have abstract methods and final fields.
In java it is possible to implement more than one interfaces by a subclass using the keywords "implements".
In the below example subclass named "SubClass.java" is extends one super class named "SuperClass.java" and implements two interfaces named "MyFirstInterface.java" and "MySecondInterface.java".
//SuparClass.java
public class SuparClass
{
public void DisplaySuperClassStatement()
{
System.out.println("I am in super class.");
}
}
//MyFirstInterface.java
public interface MyFirstInterface
{
public void DisplayMyFirstInterfaceStatement(); //nobody because of abstract method
}
//MySecondInterface.java
public interface MySecondInterface
{
public void DisplayMySecondInterfaceStatement(); //nobody because of abstract method
}
//SubClass.java
public class SubClass extends SuparClass implements MyFirstInterface,MySecondInterface
{
public void DisplayMyFirstInterfaceStatement()
{
System.out.println("I am defining DisplayMyFirstInterfaceStatement() method of MyFirstInterface.java interface");
}
public void DisplayMySecondInterfaceStatement()
{
System.out.println("I am defining DisplayMySecondInterfaceStatement() method of MySecondInterface.java interface");
}
}
//MainClass.java
public class MainClass
{
public static void main(String args[])
{
SubClass sub = new SubClass();
System.out.println("Calling SuperClass's method");
sub.DisplaySuperClassStatement();
System.out.println("\n");
System.out.println("Calling first interface's method");
sub.DisplayMyFirstInterfaceStatement();
System.out.println("\n");
System.out.println("Calling second interface's method");
sub.DisplayMySecondInterfaceStatement();
}
}
Output =>
Calling SuperClass's method
I am in super class.
Calling first interface's method
I am defining DisplayMyFirstInterfaceStatement() method of MyFirstInterface.java interface
Calling second interface's method
I am defining DisplayMySecondInterfaceStatement() method of MySecondInterface.java interface
For example in java we can't extends more than one super class for a sub class like this,
"public class subclass extends superclass1,superclass2,superclass3"
Java solve this problem using "INTERFACE". Interface is just like a java class that has variable/variables and method/methods but the only different is that interface have abstract methods and final fields.
In java it is possible to implement more than one interfaces by a subclass using the keywords "implements".
In the below example subclass named "SubClass.java" is extends one super class named "SuperClass.java" and implements two interfaces named "MyFirstInterface.java" and "MySecondInterface.java".
//SuparClass.java
public class SuparClass
{
public void DisplaySuperClassStatement()
{
System.out.println("I am in super class.");
}
}
//MyFirstInterface.java
public interface MyFirstInterface
{
public void DisplayMyFirstInterfaceStatement(); //nobody because of abstract method
}
//MySecondInterface.java
public interface MySecondInterface
{
public void DisplayMySecondInterfaceStatement(); //nobody because of abstract method
}
//SubClass.java
public class SubClass extends SuparClass implements MyFirstInterface,MySecondInterface
{
public void DisplayMyFirstInterfaceStatement()
{
System.out.println("I am defining DisplayMyFirstInterfaceStatement() method of MyFirstInterface.java interface");
}
public void DisplayMySecondInterfaceStatement()
{
System.out.println("I am defining DisplayMySecondInterfaceStatement() method of MySecondInterface.java interface");
}
}
//MainClass.java
public class MainClass
{
public static void main(String args[])
{
SubClass sub = new SubClass();
System.out.println("Calling SuperClass's method");
sub.DisplaySuperClassStatement();
System.out.println("\n");
System.out.println("Calling first interface's method");
sub.DisplayMyFirstInterfaceStatement();
System.out.println("\n");
System.out.println("Calling second interface's method");
sub.DisplayMySecondInterfaceStatement();
}
}
Output =>
Calling SuperClass's method
I am in super class.
Calling first interface's method
I am defining DisplayMyFirstInterfaceStatement() method of MyFirstInterface.java interface
Calling second interface's method
I am defining DisplayMySecondInterfaceStatement() method of MySecondInterface.java interface
No comments:
Post a Comment