You are on page 1of 6

Inheritance in Java

 a mechanism in which one object acquires all the properties and behaviours of a
parent object or when a child inherits properties of parent then it is called inheritance
 It is kind of is-a relationship between a super class and its sub classes also called
parent-child relationship.
 it means that an Object of a subclass can be used wherever an object of the super class
can be used.
 Class Inheritance in Java is used to build new Classes from existing Classes.
 The inheritance relationship is also transitive
 For example, a car Class can inherit some properties from vehicle Class.
 Here the base Class is the vehicle Class and the subclass is more specific car let i20
Class.
 A subclass must use the extends clause to derive from a super
 The subclass inherits members of the super class and hence promotes code reuse.
 The subclass itself can add its new behaviour and properties.
 For example the java.lang.Object Class is always at the top of any Class inheritance
hierarchy.
 extends keyword is used to inherit properties from parent class to child class

this and super keywords:
 this and super to help us explicitly name the field or Method that we want.
 Using this and super keyword you have full control on whether to call a Method or
field in the same Class or to call from the immediate super class. 

Terms used in Inheritance


o Class: A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created.
o Sub Class/Child Class: Subclass is a child class which inherits the properties of
other class. It is also called a derived class, extended class, or child class.
o Super Class/Parent Class: Super class is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
o Reusability:  reusability is a mechanism which facilitates us to reuse the fields and
methods of the existing class when we create a new class. We can use the same fields
and methods already defined in the previous class.

syntax to represent:

class Subclass-name extends Super class-name  
{  
   //methods and fields  
}  
 The meaning of "extends" is to increase the functionality.
 In the terminology of Java, a class which is inherited is called a parent or super class,
and the new class is called child or sub class
three types of inheritance supported in java are: single, multilevel and hierarchical.

Single Inheritance
When a class extends another one class only then we called it a single inheritance
Here below parentA is a parent class of childB and childB would be  a child class of parentA.

 Example:
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}

Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance. Here in the below
class Z (is grandchild class of X)inherits property from Y, Y inherits from X where X is a
parent class and hence construct a multilevel inheritance.

Class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}

Hierarchical Inheritance

When two or more classes inherit 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
Example:

class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class hieracl_inhrt
{
public static void main(String args[])
{
B o1 = new B();
C o2 = new C();
D o3 = new D();
//All classes can access the method of class A
o1.methodA();
o2.methodA();
o3.methodA();
o1.methodB(); //class B method()
}
}
 one class inherits features from multiple classes, it is known as multiple inheritance.

 java programming, multiple and hybrid inheritance is supported through interface

 Hybrid inheritance is a combination of Single and Multiple inheritance y

Why multiple inheritance is not supported in java?


 the concept of one class extending (Or inherits) more than one base class.

 The problem with “multiple inheritance” is that the derived class will have to manage the
dependency on more base classes.

 Multiple Inheritance is very rarely used in software projects. Using Multiple inheritance often
leads to problems in the hierarchy. This results in unwanted complexity when further
extending the class.

 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.

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?  
}  
}  
Uses of inheritance in java
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
o Static Methods or variables do not take part in inheritance.

o Even though static methods or variables do not take part in inheritance and cannot be
overridden, they can be redefined in a subclass. The redefinition is not called
overridden but hidden.

Invalid concept in Java Class Inheritance


1. Private members of the super class are not inherited by the subclass and can only be
indirectly accessed.
2. Since constructors and initializer blocks are not members of a Class, they are not
inherited by a subclass.
3. A subclass can extend only one super class
4. Members that have default accessibility in the super class are also not inherited by
subclasses in other packages, as these members are only accessible by their simple
names in subclasses within the same package as the super class.

You might also like