Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Inheritance

• Inheritance is a process to define a new class using the existing class functionality.
• We use inheritance in software development to establish the relationship between categories as per our business logic.
• Inheritance is also used for reusing the existing codes in the super class or parent class or base class.
• Using extends keyword, a class can inherit another class.
• A class that is extended from another class is called Sub class or Child Class or Derived class.
• The class from which sub classes are extended is called Super class or Parent class or Base Class.
• The sub class contains all the functionalities of its super class.
(This means we can access any member of super class from sub class directly except private members.)

Types of Inheritance: -

1. Simple Inheritance or Single Inheritance
2. Multiple Inheritance
3. Multi-Level Inheritance
4. Hierarchy Inheritance
5. Hybrid Inheritance
6. Cyclic Inheritance

1. Simple Inheritance or Single Inheritance

In a simple inheritance there will be only one sub class derived from one super class.

            
class JTC{
	public static void main(String arg[]){
		System.out.println("main in JTC class");
		B b1 = new B();
		b1.m1();
		System.out.println("b1.a :- "+b1.a);
	}
}
class A{
	int a = 10;
}
class B extends A{
	void m1(){
		System.out.println("m1 in B class");
		System.out.println("a :- "+a);
	}
}

        
    main in JTC class
    m1 in B class
    a :- 10
    b1.a :- 10
            

In the above example we have a class A and it sub-class which is B. In B sub class we have a method which is void m1() where we are accessing the value of int a = 10 which is the member of A class. Since a sub-class contains all the functionalities of its super class hence we are able to access the value of int a = 10 directly from B class. In the above example we are also accessing the value of int a = 10 from JTC class along with B class object, this also proves that B class has all the functionalities of its super class A.

            
            class JtcStudent{
	int sid;
	String sname;
	String semail;
	
	void createStudent(int sid, String sname, String semail){
		System.out.println("------STORING STUDENT BASIC DETAILS------");
		this.sid = sid;
		this.sname = sname;
		this.semail = semail;
	}
	void showDetail(){
		System.out.println("-----Student BasicDetails-----");
		System.out.println("SID :- "+sid);
		System.out.println("SNAME :- "+sname);
		System.out.println("SEMAIL :- "+semail);
	}
}
class CurrentStudent extends JtcStudent{
	String batchId;
	String BatchType;
	String LmsAccess;
	
	void newStudent(int sid, String sname, String semail, String batchId, String BatchType,String LmsAccess){
		System.out.println("----CREATING NEW STUDENT------");
		createStudent(sid,sname,semail);
		this.batchId = batchId;
		this.BatchType = BatchType;
		this.LmsAccess = LmsAccess;
	}
	void currentStudentDetails(){
		showDetail();
		System.out.println("------Current Student Details -------");
		System.out.println("BatchId :- "+batchId);
		System.out.println("BetchType :- "+BatchType);
		System.out.println("LmsAccess :- "+LmsAccess);
	}
}
class JTC{
	public static void main(String arg[]){
		CurrentStudent s1 = new CurrentStudent();
		s1.newStudent(101,"Vivek","vivek@jtcindia.org","JFS-B-12","Weekdays","Assessed");
		s1.currentStudentDetails();
	}
}

        
    ----CREATING NEW STUDENT------
    ------STORING STUDENT BASIC DETAILS------
    -----Student BasicDetails-----
    SID :- 101
    SNAME :- Vivek
    SEMAIL :- vivek@jtcindia.org
    ------Current Student Details -------
    BatchId :- JFS-B-12
    BetchType :- Weekdays
    LmsAccess :- Assessed
            

The above example helps us to understand that how we can use inheritance concept to establish the relationship between categories as per our business logic. In the program we have three classes: JtcStudent , CurrentStudent and JTC. JtcStudent class represents a set of all students enrolled in JTC, including passed out students and current students. CurrentStudent class represents all students who are presently undergoing training at JTC. CurrentStudent class has been extended from JtcStudent class. As we can see CurrentStudent class has its own information which are batchId, BatchType and LmsAccess and a student included in the CurrentStudent class is also included in JTC class. Hence, for any student, all information regarding the student from JtcStudent class would also be required.

2. Multiple Inheritance: -

• In Multiple Inheritance a class inherits more than one class at a time.
• In Java programming language we cannot carry out multiple inheritance using classes due to the ambiguity problem.

            
class A{
	int a = 10;
}
class B{
	int a = 20;
}
/*class C extends A,B{
	void m1 (){
		System.out.println("a :- "+a);	
}
}*/

        

In the above example we have C class that is extending more than one classes which are A and B, which is not allowed in Java. As we can see that in C class, we have a method which is m1 () method. In m1 () method we are accessing the value of a variable. In both the classes A and B we have that variable with the same name. In C class m1(), the JVM has no clear instructions as to from which class the value of a variable has to be obtained. This problem is called ambiguity problem and due to the problem we can’t carry out multiple inheritance process using classes.

Note: - In Java using Interface we can easily carry out multiple inheritance and solve the ambiguity problem.

3. Multi-Level Inheritance

In this case, there will be only one immediate super class but many indirect super and subclasses.

            
class JTC{
	public static void main(String arg[]){
		C c1 = new C();
		System.out.println("c1.a :- "+c1.a);
		System.out.println("c1.b :- "+c1.b);
		System.out.println("c1.c :- "+c1.c);
	}
}
class A{
	int a = 10;
}
class B extends A{
	int b = 20;
}
class C extends B{
	int c = 30;
}


        
    c1.a :- 10
    c1.b :- 20
    c1.c :- 30
            

In this example we can see that we have four classes: JTC, A, B and C. C class is extending B class and B class is extending A class. Hence C class has one immediate super class which is B, A is the indirect super class of C. That is the reason using the C class object we are able to access the value of int a = 10 (Member of A class), int b = 20 (Member of B class) and int c = 30 (Member of C class itself).

4. Hierarchy Inheritance

• In Hierarchy Inheritance more than one class extends the same class.
• In this type there will be only one immediate super class and many immediate subclasses.

            
class JTC{
	public static void main(String arg[]){
		B b1 = new B();
		System.out.println("b1.a :- "+b1.a);
		System.out.println("b1.b :- "+b1.b);
		C c1 = new C();
		System.out.println("c1.a :- "+c1.a);
		System.out.println("c1.c :- "+c1.c);
	}
}
class A{
	int a = 10;
}
class B extends A{
	int b = 20;
}
class C extends A{
	int c = 30;
}


        
    b1.a :- 10
    b1.b :- 20
    c1.a :- 10
    c1.c :- 30
            

In the above example we can see the relation between B and A class and C and A class. Here both the classes B and C are extending the same class which is A. That is the reason in the JTC class main method, using B class object we are able to access int b = 20 which is the member of B class itself; as well as we are accessing int a = 10 which is the member of A class and the same process is taking place with C class object as well.

5. Hybrid Inheritance

• Hybrid Inheritance includes all the above types of inheritance which are: Simple Inheritance, Multiple Inheritance, Multi-Level Inheritance and Hierarchy Inheritance.
• In the other words we can say that Hybrid Inheritance is combination of different types of inheritance.
• In Hybrid Inheritance there can be Multiple Inheritance as well. As we know that in Java we can’t carry out Multiple Inheritance with classes due to the ambiguity problem. Therefore, in Java we can’t carry out Hybrid Inheritance using classes.

6. Cyclic Inheritance

• In Cyclic Inheritance a class is inherited from itself or two different classes are extended from each other.
• In Java programming language cyclic inheritance in not allowed due to the invocation of the cyclic constructor using the super statement (super()).

            
// A class is extending itself.

class A extends A{
}

// Here B class is extending C class and again C class is extending B class.

class B extends C{
}
class C extends B{
}

        
    test.java:4: error: cyclic inheritance involving A
    class A extends A{
    ^
    test.java:9: error: cyclic inheritance involving B
    class B extends C{
    ^
    2 errors
    error: compilation failed
            

In the above example we can see we that have a class which is A. A class is extending from itself and in the other section we have two different classes B and C which are extending to each other.

As we know that cyclic inheritance is not allowed in Java. Hence in this code we will get two errors at compile time, which appear as: cyclic inheritance involving A and cyclic inheritance involving B respectively.

Super Statement

• In Java super () is basically use to invoke immediate super class constructor from the sub class constructor.
• If we do not define any super statement in the constructor, then JVM auto defines a default super statement [super ()] in the constructor.
• A super statement [super ()] always will be the first statement inside the constructor, or else we will get an error at the time of compilation.
• When we are defining a super statement inside the constructor then we must keep in mind that super() must be the first statement inside the constructor.

            
class JTC{
	public static void main(String arg[]){
		C c1 = new C();
	}
}
class A{
	A(){
		System.out.println("A() cons in A class");
	}
}
class B extends A{
	B(){
		System.out.println("B() cons in B class");
	}
}
class C extends B{
	C(){
		System.out.println("C() cons in C class");
	}
}


        
    A() cons in A class
    B() cons in B class
    C() cons in C class
            

In the above example we can see that C class is extending B class and B class is extending A class, as we know that JVM defines super statement with no argument in every constructor which invokes immediate super class constructor. That is the reason first A class constructor is processed, then B class constructor is processed and finally C class constructor is processed.

            
class JTC{
	public static void main(String arg[]){
		B b1 = new B();
	}
}
class A{
	A(int a){
		System.out.println("A(int a) in A class");
	}	
}
class B extends A{
	B(){
		System.out.println("B() in A class");
	}
}

        
    Test1.java:12: error: constructor A in class A cannot be applied to given types;
    B(){
    ^
    required: int
    found:    no arguments
    reason: actual and formal argument lists differ in length
    1 error
    error: compilation failed
            

In this example we can see that in A class there is only one constructor A with int type parameter. B class constructor contains super() which can invoke a default constructor; however in A class there is no default constructor, so we will get an error at compile time.

            
class JTC{
	public static void main(String arg[]){
		B b1 = new B();
	}
}
class A{
	A(int a){
		System.out.println("A(int a) in A class");
	}	
}
class B extends A{
	B(){
		super(10);
		System.out.println("B() in A class");
	}
}
        
    A(int a) in A class
    B() in A class
            

In the above example we can see in B class constructor we have super (10) that invokes immediate super class constructor A(int a).

super Keyword

super is a keyword in Java. In Java we use super keyword to access member of the immediate super class from sub class. We cannot use super keyword in an static context.

            
class JTC{
	public static void main(String arg[]){
		B b1 = new B();
		b1.m1();
	}
}
class A{
	int a = 10;
}
class B extends A{
	String a = "JTC";
	
	void m1(){
		System.out.println("m1 in B class");
		System.out.println("a :- "+a);
		System.out.println("a :- "+super.a);
	}
}

        
    m1 in B class
    a :- JTC
    a :- 10
            

In the above example we can see that from m1 method of B class we are accessing the value of a directly; that is the reason we are getting the value from String a = “JTC”, because String a = “JTC” is declared in B class directly. In the next line we are accessing the value of a with the help of a super keyword. As we know that using a super keyword, we can access the value of super class from sub class; that is the reason we are getting the value from int a = 10 which is the member of A class.