Inheritance Concepts In Java
Inheritance Concepts In Java
Inheritance
One of OOPs’ strength is Inheritance. Inheritance allows the creation of hierarchical classifications’ of classes.
Inheritance means, one can create a general class that defines certain properties common to a set of related items, and then this class can be inherited by other classes, each adding those properties.
Super Class :In java, a class that is inherited is called super class.
Sub Class : In java, the class that does the inheriting is called a subclass. In other words, a sub class is a specialized version of a super class. It inherits all of the permitted instance variables and permitted member methods, defined by the super class and adds its own, unique elements.
The super class also can be used to create an object in main () method, further a subclass can be a super class for another subclass.
To inherit a class, simply use extends key word
The general form of a class declaration that inherits super class
class subclass-name extends superclass-name{
// body of class.
}
Remember:
ü You can specify only one super class for any subclass.(java does not support multiple class inheritance, into a single sub class, This differs from C++, in which you can inherit multiple base classes, into a single derived class). Thus java does not support multiple inheritance but supports only multi level inheritance.
ü No class can be a super class of it self.
/*
This program illustrates the power of inheritance. class Box is a super class and the sub classBoxWeight inherits the Box class, by using the extends keyword
*/
class Box{
double width,height,depth;
//constructor used when no dimensions specified.
Box(){
width = height = depth = -1;
}
// constructor used when all dimensions specified.
Box(double w, double h, double d){
width = w;height = h;depth = d;
}
//constructor used when cube is created.
Box(double side){
width = height = depth = side;
}
// construct clone of an object
Box(Box obj){
width = obj.width;
height = obj.height;
depth = obj.depth;
}
// compute return volume
double volume (){
return depth * height * depth;
}
}
// here, Box is extended to include weight
class BoxWeight extends Box{
double weight; // weight of Box
// constructor for BoxWeight class
BoxWeight(double w, double h, double d, double m){
width = w;
height = h;
depth = d;
weight = m;
}
}
class BoxWeightDemo{
public static void main(String args[]){
BoxWeight MyBox1 = new BoxWeight(23.33,33.33,43.33,50);
BoxWeight MyBox2 = new BoxWeight(33.33,43.33,53.33,150);
double vol = MyBox1.volume();
System.out.println(“volume of MyBox1 is : “+vol);
System.out.println(“weight of MyBox1 is : “+MyBox1.weight+” kgs”);
vol = MyBox2.volume();
System.out.println(“volume of MyBox2 is : “+vol);
System.out.println(“weight of MyBox2 is : “+MyBox2.weight+” kgs”);
}
}
Explanation : A major advantage of inheritance is that once a class is created it can be used, as a super class to create any number of subclasses and each subclass can precisely tailor its own classification. Now consider this example, which again makes use of Box class.
/* A class inherits Box and adds color attribute */
/* This program illustrates the power of inheritance */
class Box{
double width,height,depth;
//constructor used when no dimensions specified.
Box(){
width = height = depth = -1;
}
// constructor used when all dimensions specified.
Box(double w, double h, double d){
width = w;
height = h;
depth = d;
}
//constructor used when cube is created.
Box(double side){
width = height = depth = side;
}
// construct clone of an object
Box(Box obj){
width = obj.width;
height = obj.height;
depth = obj.depth;
}
// compute return volume
double volume (){
return depth * height * depth;
}
}
// here, Box is extended to include color
class BoxColor extends Box{
String color; // color of Box
// constructor for BoxColor class
BoxColor(double w, double h, double d, String str){
width = w;
height = h;
depth = d;
color =str;
}
}
class BoxColorDemo{
public static void main(String args[]){
BoxColor MyBox1 = new BoxColor(23.33,33.33,43.33,”RED”);
BoxColor MyBox2 = new