inheritance_notes
inheritance_notes
The idea of inheritance means, already you have some engineered product, and you are
making a similar one and you want to add some extra features so whatever the work
you have done, you don’t have to do it again here, just take it is and add extra features.
Inheritance means creating a new class based on existing class.
Inheritance (Blood relation) – is a relationship (100%)
It is one of the ways to reusability in java.
It is a process of acquiring the property and behavior of parent (Base, Super) class
into a new class child class.
In java we use extends keyword to inherit a class.
Whenever inheritance is done all data and functions are accessible in child class.
ADVANTAGES:
class B
{
1. Reusability
2. To achieve run-time polymorphism. }
class C extends B
3. Data hiding {
4. Method overriding }
Parent class
Animal
-eat()
-walk()
-sleep()
Types of Inheritance:
1. Single level Inheritance
2. Multi-level Inheritance
3. Multiple Inheritance
4. Hybrid Inheritance
5. Hierarchical or Network Inheritance
Single level Inheritance
When a class inherits another class, it is known as a single inheritance. In the example given below,
Dog class inherits the Animal class, so there is the single inheritance.
Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
class A {
public void test1() {
System.out.println("1");
}
}
class B extends A{
public void test2(){
System.out.println("2");
}
}
class C extends B {
public void test3() {
System.out.println("3");
}
public static void main(String[] args) {
C obj = new C();
obj.test1();
obj.test2();
obj.test3();
}
}
Hierarchical Inheritance
When two or more classes inherits a single class, it is known as hierarchical
inheritance. In the example given below, Dog and Cat classes inherits the Animal class,
so there is hierarchical inheritance.
package Inheritance;
public class Animal {
public void eat() {
System.out.println("Eating...");
}
Parent Class
package Inheritance;
dog.sleep();
dog.walk();
dog.noise(); // Added extra features in dog class
}
public void noise() {
System.out.println("Bow Bow..");
}
}
package Inheritance;
cat.sleep();
cat.walk();
cat.noise();
}
public void noise() {
System.out.println("Meow Meow...");
}
}
Multiple Inheritance
When one class inherits multiple classes, it is known as multiple inheritance.
Note: Multiple inheritance is not supported in Java through class because of *Diamond
problem.
*Diamond problem: when same method is inherited to child class twice resulting in confusion of
inheritance hierarchy.
Hybrid Inheritance
In java we can hold the child class object into reference variable of parent class. This concept is called
upcasting.
When we upcast, we can only access the over-ridden methods but not all the methods of the child
class.
class parent
{
}
class Child extends Parent
{
}
Parent obj = new Child(); //upcasting
Method Overriding
Sometimes a child class may not be satisfied with parent method implementation then the child class
is allowed to redefine that method based on its requirement, such type of concept is called method
overriding.
method overriding is a technique in which child class adds a method that is already present in its
parent class and adds new functionality to this method. Then we say that the base class method is
overridden.
Rules for Java Method Overriding
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
When parent class and child class have same name of method with same signature (method
name, return type, and parameters). So, we can say that child class overrides the parent class
method.
When method overriding is done, always preference goes to child class overridden
method.
Syntax: The subclass method is annotated with @Override, although the annotation is optional.
Compile-Time Checking: Ensures the method actually overrides a superclass method or not (check
method signature of parent and child class), otherwise it generates a compile-time error.
class Test {
public static void main(String[] args) {
Parent p = new Parent();
p.marry(); // Output: Rajlaxmi (calls Parent's marry() method)
Parent p1 = new Child(); // Upcasting: p1 is a reference of type Parent, but refers to a Child object
p1.marry(); // Output: Katrina Kaif (due to runtime polymorphism, Child's marry() method is called)
// p1.career(); // Compilation error: The method career() is undefined for the type Parent
Explanation: we are calling marry () method on p1, p1 is type of Parent type, compiler is always going to
check in parent class the marry () method is there or not. In the parent class if marry () method is not
present, gives compile time error, if method is present then perfectly fine. Now at run time JVM is always
going to check in the child class is marry () method is overridden or not, if overridden, priorities go to
child class hence child class method called. (this concept is called runtime polymorphism)
At runtime, Java uses the actual object type (Child) to decide which method to call. This is known as
dynamic method dispatch or runtime polymorphism.
Ans: In the case of method overriding, the method call is resolved dynamically at runtime. Hence it is
called dynamic polymorphism.