☰ See All Chapters |
Inheritance in Java
A class should be closed for modification but open for extension. We should not modify any class; we can add additional features to an existing class without modifying it. Inheritance is one concept to achieve this. Inheritance is a concept of inheriting the properties of one class (super class) into another class (sub class) when there “IS” a relationship between classes. It is a process of reusing existing class functionality in new class. The existing class is called super class/parent class/base class and the new class is called subclass/child class/derived class. All the members of super class will be inherited into subclass (except constructor and private members) and can be accessed directly in subclass. Super classes are generalized classes, subclasses are specialized classes. Super class, subclass and client class all should be in same folder; otherwise need to deal with packages.
Generalization
Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass
Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behavior are used from the specialization to the generalization class. At a very broader this is an inheritance concept.
Specialization
Specialization means creating new special subclasses from an existing class.
Syntax to create subclass
class SubclassName extends SuperclassName {
//Members of subclass
}
Different types of Inheritance
Simple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
1) Simple Inheritance
In simple inheritance there will be exactly one super class and one subclass i.e. subclass will get the functionality from exactly only one super class.
It is supported by class data type.
class Box { double width; double height; double depth;
// constructor clone of an object, used to copy values from one object to // another object. Box(Box ob) {// pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; }
// constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; }
// constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box }
// constructor used when cube is created Box(double len) { width = height = depth = len; }
// compute and return volume double volume() { return width * height * depth; } }
class BoxWeight extends Box { double weight;
BoxWeight(double w, double h, double d, double m) { width = w; height = h; depth = d; weight = m; } }
class DemoBoxWeight { public static void main(String args[]) { Box supbox1 = new Box(1, 2, 3); Box supbox2 = new Box(supbox1); // This will be error if constructor clone is not present double vol; vol = supbox1.volume(); System.out.println("Volume of supbox1 is " + vol); vol = supbox2.volume(); System.out.println("Volume of supbox2 is " + vol); BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); System.out.println(); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.weight); } } |
Output:
Volume of supbox1 is 6.0 Volume of supbox2 is 6.0 Volume of mybox1 is 3000.0 Weight of mybox1 is 34.3
Volume of mybox2 is 24.0 Weight of mybox2 is 0.076 |
2) Multilevel Inheritance
In multilevel inheritance one super class can have many sub classes.
One subclass can have many indirect super classes i.e. one is direct super class all remaining are indirect super classes.
It is supported by class data type.
class Aves{ public void nature() { System.out.println("Generally, Aves fly"); } } class Bird extends Aves{ public void eat(){ System.out.println("Eats to live"); } } public class Parrot extends Bird{ public void food(){ System.out.println("Parrot eats seeds and fruits"); } public static void main(String args[]){ Parrot p1 = new Parrot(); p1.food(); // calling its own p1.eat(); // calling super class Bird method p1.nature(); // calling super class Aves method } } |
Output:
Parrot eats seeds and fruits Eats to live Generally, Aves fly |
3) Hierarchical Inheritance
In this one super class can have many direct sub classes.
It is supported by class data type.
class Aves{ public void fly() { System.out.println("Generally, aves fly"); } } class Parrot extends Aves{ public void eat(){ System.out.println("Parrot eats fruits and seeds"); } } class Vulture extends Aves{ public void vision(){ System.out.println("Vulture can see from high altitudes"); } } public class FlyingCreatures{ public static void main(String args[]){ Parrot p1 = new Parrot(); p1.eat(); p1.fly(); Vulture v1 = new Vulture(); v1.vision(); v1.fly(); } } |
Output:
Parrot eats fruits and seeds Generally, aves fly Vulture can see from high altitudes Generally, aves fly |
4) Multiple Inheritances
In this one sub class can have many direct super classes.
Multiple inheritance is not supported by java with class data type. But it is supported with interface data type
We can achieve partial multiple inheritance with the help of interfaces.
Example:
public class FerrariF12011 extends Ferrari implements Car, Automobile {//…}
5) Hybrid Inheritance
It is the combination of multiple, multilevel and hierarchical inheritance.
Hybrid inheritance is not supported by java with class data type. But it is supported with interface data type.
Example:
public class FerrariF12011 extends Ferrari implements Car, Automobile {//…}
All Chapters