You are on page 1of 10

Inheritance

Inheritance in Java
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 you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Example –
Types of Inheritance Class Parent {
int x = 10; Child object “ob” can able to
1. Single level inheritance } access properties of Parent class
variable “x”. Because Child class
Parent Base class Class Child extends Parent { grasps all the properties of the
class void display() parent class.
{
System.out.println(“Child class method..”)
Child class Derived
;
class
} }
Class USE {
public static void main(String args[]) {
Child ob = new Child();
System.out.println(“Value of x”+ ob.x);
ob.display() ; }
}
2. Multilevel inheritance

A Base class
Example -
class Animal{  
B 1st Derived void eat(){System.out.println("eating...");}  
class }  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
C 2nd Derived }  
class class BabyDog extends Dog{  
void weep(){System.out.println("weeping...");}  
}  
When there is a chain of inheritance, it is class TestInheritance2{  
known as multilevel inheritance. As you can public static void main(String args[]){  
see in the example given below, BabyDog BabyDog d=new BabyDog();  
class inherits the Dog class which again d.weep();  
inherits the Animal class, so there is a d.bark();  
multilevel inheritance. d.eat();  
}}  
3. Hierarchical Inheritance
Example –

class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("meowing...");}  
When two or more classes inherits a single }  
class, it is known as hierarchical inheritance. class TestInheritance3{  
In the example given below, Dog and Cat public static void main(String args[]){  
classes inherits the Animal class, so there is Cat c=new Cat();  
hierarchical inheritance. c.meow();  
c.eat();  
}}  
Why 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.

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 invoked?  
}  
}  
Method Overriding

The idea behind method overriding is that if the function defined in Parent class. The same function is
redefine in child class. And we invoke the function using child class object. The child class function
overrides the parent class function.

Example –
Class Parent {
void display()
{
System.out.println(“Parent class method..
”); When we run this program the output is “Child
}} class method”. It means the child class object
Class Child extends Parent { overrides the parent class method.
void display()
{
System.out.println(“Child class method..”)
;
} }
Class USE {
public static void main(String args[]) {
Child ob = new Child();
ob.display() ; }
}
Super Keyword in Java
The super keyword in Java is a reference variable which is used to refer immediate parent
class object.
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.

Usage of Java super Keyword

1.super can be used to refer immediate parent class instance variable.


2.super can be used to invoke immediate parent class method.
3.super() can be used to invoke immediate parent class constructor.
 Super is used to refer immediate parent class instance variable.

We can use the super keyword to access the data member or field of parent class. It is used if parent class and
child class have same fields.

class Animal{  
String color="white";  
}  
class Dog extends Animal{  
String color="black";  
void printColor(){  
System.out.println(color); //prints color of Dog class  
System.out.println(super.color); //prints color of Animal class  
}  
}  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}}  
super can be used to invoke the parent class method

The super keyword can also be used to invoke parent class method. It should be used if subclass contains
the same method as parent class. In other words, it is used if method is overridden.

Class parent {
void display()
{ System.out.println (“Method of parent class”); }
}
Class Child extends parent {
void display() {
super.display();
System.out.println (“Method of child class”); }
}

Class USE
{ Output:
pubic static void main(String args[]) {
Child ob = new Child(); Method of parent class
ob.display(); Method of child class
}
}
Super is used to invoke parent class constructor Class USE
{ pubic static void main(String args[]) {
Class A {
B ob = new B();
A()
} }
{ System.out.println (“class A default constructor”
); Output –
} class A default constructor
A(String str) class B default constructor
{ System.out.println (str); Because constructor B inherits parent class constructor
} also.
} Class USE
{ pubic static void main(String args[]) {
Class B extends A { B ob = new B(“hello”);
B() } }
{ System.out.println (“class B default constructor”
Output –
);
Hello
} Because first B(String str) constructor is invoked. And as the
B (String str) super(str) statement encounters the control goes to A(String
{ super(str); } str) constructor of super class.
}

You might also like