Inheritance in Java allows classes to acquire properties and behaviors of parent classes. The extends keyword indicates that a subclass inherits from a superclass. There are different types of inheritance in Java including single inheritance, multilevel inheritance, and hierarchical inheritance, but multiple inheritance is not supported. Interfaces can be used as a workaround for multiple inheritance by implementing multiple interfaces.
2. INHERITANCE
Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object.
The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes. When we inherit from an existing class, we can
reuse methods of the parent class. Moreover, you can add new methods in
your current class also.
For Code Reusability.
3. THE SYNTAX OF JAVA INHERITANCE
Class Superclass-name
{
//methods
}
class Subclass-name extends Superclass-name
{
//methods
}
//Keyword
//extends
The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to increase
the functionality.
6. SINGLE INHERITANCE
class Parent //Parent Class...
{
void show() // Function declaration
{ System.out.println("Show() of class Parent"); }
}
class child extends Parent // Child is inheriting the properties of Parent class
{
public static void main(String arg[])
{ System.out.println("Hello from class child");
child obj = new child(); // Object creation
obj.show();
}
}
OUTPUT
7. MULTILEVEL INHERITANCE
class version{
void version(){System.out.println("version");}
}
class version1 extends version{
void version1(){System.out.println("version1");}
}
class version2 extends version1
{ public static void main(String args[]){
version2 d= new version2();
d.version(); d.version1();d.version2();
}
void version2(){System.out.println("version2");}
}
OUTPUT
8. HIERARCHICAL INHERITANCE EXAMPLE
class version{ void version(){System.out.println("version");} }
class version1 extends version{ void version1(){System.out.println("version1");} }
class version2 extends version{void version2(){System.out.println("version2");}}
class all
{
public static void main(String args[])
{ version1 d1= new version1();
version2 d2= new version2();
d1.version(); d1.version1();
d2.version(); d2.version2();
}
}
OUTPUT
9. WHY MULTIPLE INHERITANCE IS NOT
SUPPORTED IN JAVA?
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A
and B classes. If A and B classes have the same method and you call it from
child class object, there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-
time error if you inherit 2 classes. So whether you have same method or
different, there will be compile time error.
PROGRAM:
class A
{ void msg(){System.out.println("Hello");}
}
Class B{ void msg(){System.out.println("Welcome");}}
class C extends A,B //suppose if it were
{ public static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoke
d?
}
}
OUTPUT Compile Time Error
Which mean Hybrid Inheritance is also not possible.
?That how is this possible?
If we need multiple or hybrid
inheritance
10. INTERFACE
interface one
{ void show(); int a=10; }
interface one1
{ void show(); int b=10; }
class inth implements one,one1
{ public static void main(String arg[])
{ System.out.println("hlo from main");
inth ob= new inth();
ob.show(); }
public void show() // should be public other vice error
{System.out.println("sum is >>>>"+(a+b));}
}
OUTPUT