You are on page 1of 13

Inheritance

Inheritance
• It is a mechanism by which one class allows to inherit the features (instance variables and
methods) of another class.
• A class that is inherited is called a superclass and the class that does the inheriting is called
a subclass.
• A subclass is a specialized version of a superclass.
• It inherits all of the instance variables and methods defined by the superclass and adds its
own, unique elements.
Syntax
class super1 { class Employee{  
 float salary=40000;  
data members1;
Employee(){
member functions1(){ salary = 0;
} }
} } 
class sub1 extends super1{
 class Programmer extends Employee{
data members2;
   int bonus=10000;
member functions2(){ Programmer(){
} bonus = 0;
} }   
}

class demoEmployee{
public static void main(String args[]){ 
Programmer p=new Programmer();
    
 
System.out.println("Programmer salary is:"+p.salary);   
  
System.out.println("Bonus of Programmer is:"+p.bonus);  

 }
// This program uses inheritance to extend // constructor used when no
Box. dimensions specified
class Box { Box() {
double width;
width = -1;
double height;
height = -1;
double depth;
// construct clone of an object depth = -1;
Box(Box ob) { }
width = ob.width; // constructor used when cube is
height = ob.height; created
depth = ob.depth; Box(double len) {
} width = height = depth = len;
// constructor used when all }
dimensions specified
// compute and return volume
Box(double w, double h, double d) {
double volume() {
width = w;
height = h; return width * height * depth;
depth = d; }
} }
// Here, Box is extended to include class DemoBoxWeight {
weight. public static void
main(String args[]) {
class BoxWeight extends Box { BoxWeight mybox = new
// weight of box BoxWeight(10, 20, 15, 34.3);
double weight; double vol;
vol = mybox.volume();
// constructor for BoxWeight
BoxWeight(double w, double h, System.out.println("Volume
double d, double m) { of mybox is " + vol);
width = w;
height = h; System.out.println("Weight
depth = d; of mybox is " + mybox.weight);
weight = m;
} }
} }
A superclass variable can reference a subclass object
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is " + vol);
System.out.println("Weight of weightbox is " + weightbox.weight);
// assign BoxWeight reference to Box reference
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox does not define a weight
member. */
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
}
Using super
• Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.

• Example: Use of super in Boxweight() class:

class BoxWeight extends Box {


double weight; // weight of box
// initialize width, height, and depth using super()
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
}

• This causes the Box( ) constructor to be called, which initializes width, height, and depth
using these values.

• BoxWeight no longer initializes these values itself. It only needs to initialize the value
unique to it: weight.

• This leaves Box free to make these values private if desired.


A complete implementation of BoxWeight
class Box { // BoxWeight now fully implements all constructors.
private double width;
class BoxWeight extends Box {
private double height;
private double depth;
double weight; // weight of box
// construct clone of an object // construct clone of an object
Box(Box ob) { // pass object to constructor BoxWeight(BoxWeight ob) {
width = ob.width; // pass object to constructor
height = ob.height;
super(ob);
depth = ob.depth;
}
weight = ob.weight;
// constructor used when all dimensions specified }
Box(double w, double h, double d) { // constructor when all parameters are specified
width = w; BoxWeight(double w, double h, double d, double m) {
height = h; super(w, h, d); // call superclass constructor
depth = d;
weight = m;
}
// constructor used when no dimensions specified }
Box() { // default constructor
width = -1; // use -1 to indicate BoxWeight() {
height = -1; // an uninitialized super();
depth = -1; // box
weight = -1;
}
// constructor used when cube is created
}
Box(double len) { // constructor used when cube is created
width = height = depth = len; BoxWeight(double len, double m) {
} super(len);
// compute and return volume weight = m;
double volume() {
}
return width * height * depth;
} }
}
class DemoSuper { vol = mybox3.volume();
public static void main(String args[]) { System.out.println("Volume of mybox3 is " + vol);
BoxWeight mybox1 = new BoxWeight(10, 20, 15, System.out.println("Weight of mybox3 is " +
34.3); mybox3.weight);
System.out.println();
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
vol = myclone.volume();
BoxWeight mybox3 = new BoxWeight(); // default System.out.println("Volume of myclone is " + vol);
BoxWeight mycube = new BoxWeight(3, 2); System.out.println("Weight of myclone is " +
BoxWeight myclone = new BoxWeight(mybox1); myclone.weight);
double vol; System.out.println();
vol = mybox1.volume(); vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mycube is " +
System.out.println("Weight of mybox1 is " + mycube.weight);
mybox1.weight); System.out.println();
System.out.println(); }
vol = mybox2.volume(); }
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " +
mybox2.weight);
System.out.println();
Types of Inheritance
• On the basis of class, there can be three types of inheritance in java:
– single,
– multilevel
– hierarchical
• Multiple and hybrid inheritance is supported through interface only
(We will learn about interfaces later)
Questions
1. Write a program depicting Single inheritance where
Rectangle (instance variables: length, breadth) class inherits
only one parent class Shape (instance variable: area, member
function: area)
2. Write a program depicting Multilevel inheritance having
three classes – Person (instance variables: nationality, place),
Employee (instance variables: organization, workPlace) and
Manager (instance variables: subordinates). Display each of
the instance variables using member functions.
3. Write a program depicting Heirarchical inheritance having
four classes – BasicScience (instance variables: physics,
chemistry, maths) , ComputerScience (instance variables:
compiler design) , InformationTechnology (instance
variables: crpytography) and Electronics (instance variables:
digital logic). Display each instance variable using member
functions.
Q. Order of calling of constructors in multilevel
inheritance
• When a class hierarchy is created, in what
order are the constructors for the classes that
make up the hierarchy called?
• For example, given a subclass called B and a
superclass called A, is A’s constructor called
before B’s or vice versa?
• Write a Java program to illustrate the order in
which constructors are called in a multilevel
inheritance.
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}

You might also like