Inheritance
Inheritance in Java
Classes in Java exist in a hierarchy. A class in Java can be declared as a subclass of another class using the extends keyword. A subclass inherits variables and methods from its superclass and can use them as if they were declared within the subclass itself:Important terminology in inheritance
Super Class: The class whose features are inherited is known as super class(or a base class or a paren class).Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Inheritance represents the IS-A relationship, also known as parent-child relationship.The keyword used for inheritance is extends.
Syntax
class derived-class extends base-class
{
//methods and fields
}
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.
Use of inheritance in Java
1. For Method Overriding (so runtime polymorphism can be achieved).2. For Code Reusability.
Program
class Animal
{
int weight;
String name;
void walking()
{
System.out.println("Animal is walking");
}
}
//extends the properties of superclass Animal
class Cat extends Animal
{
}
public class Main
{
public static void main(String args[])
{
Cat c = new Cat();
// Accessing variable of superclass with instance of subclass
c.name = "minii";
c.weight = 8;
System.out.println("name = " + c.name + " weight = " + c.weight);
// Accessing method of superclass with instance of subclass
c.walking();
}
}
Output
name = minii weight = 8
Animal is walking
Explanation
As displayed in the above program, Cat is the subclass and Animal is the superclass. Relationship between two classes is Cat IS-A Animal.It means that Cat is a type of Animal.In the above example, Cat object can access the field and method of own class as well as of Animal class i.e. code reusability.
Types of inheritance in Java
Below are the different types of inheritance which is supported by Java. 1. Single Inheritance : In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B.Single inheritance example in Java
Program
class Creature
{
void eating()
{
System.out.println("Eating...");
}
}
//extends the properties of superclass Creature
class Cat extends Creature
{
void walking()
{
System.out.println("Walking...");
}
}
public class Main
{
public static void main(String args[])
{
Cat c = new Cat();
// Accessing method of superclass with instance of subclass
c.eating();
c.walking();
}
}
Output
Eating...
Walking...
![](https://www.javatpoint.com/images/core/typesofinheritance.jpg)
2. Multilevel Inheritance : In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.
Multlevel inheritance example in Java
Program
class grand_parent
{
void gp()
{
System.out.println("I am grand parent");
}
}
//extends the properties of superclass grand_parent
class parent extends grand_parent
{
void p()
{
System.out.println("I am parent");
}
}
//extends the properties of superclass parent
class child extends parent
{
void c()
{
System.out.println("I am child");
}
}
public class Main
{
public static void main(String args[])
{
child c = new child();
// Accessing method of superclass with instance of subclass
c.c();
c.p();
c.gp();
}
}
Output
I am child
I am parent
I am grand parent
3. Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class.In below image, the class A serves as a base class for the derived class B and C.
Program
class Creature
{
void eating()
{
System.out.println("Eating...");
}
}
//extends the properties of superclass Creature
class Cat extends Creature
{
void walking()
{
System.out.println("Walking...");
}
}
//extends the properties of superclass Creature
class crow extends Creature
{
void flying()
{
System.out.println("Flying...");
}
}
public class Main
{
public static void main(String args[])
{
Cat c = new Cat();
crow c2 = new crow();
// Accessing method of superclass with instance of subclass
c.eating();
c.walking();
c2.eating();
c2.flying();
}
}
Output
Eating...
Walking...
Eating...
Flying...
4. Multiple Inheritance (Through Interfaces) : In Multiple inheritance ,one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. In image below, Class C is derived from interface A and B.
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 same method and you call it from child class object, there will be ambiguity to call 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 now.Program
class Creature
{
void eating()
{
System.out.println("Creature");
}
}
class Animal
{
void walking()
{
System.out.println("Animal");
}
}
//for example purpose
class crow extends Creature,Animal
{
void bird()
{
System.out.println("Animal and Creature");
}
}
public class Main
{
public static void main(String args[])
{
}
}
Output
compile time error occurs
5.Hybrid Inheritance(Through Interfaces) : It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.
![](https://www.javatpoint.com/images/core/multiple.jpg)
Important facts about inheritance in Java
Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class.Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java.
Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods(like getters and setters) for accessing its private fields, these can also be used by the subclass.
Comments
Post a Comment