Inheritance

Inheritance is the most important principle of Object Oriented Programming as it allows us to create the hierarchical classification of classes.

By using this we create a general class have some basic properties which can be inherited by other classes.

The class which inherited by other is called super class, which inherits is calledsub class.

Sub class is the specialized version of super class as it inherits all the members that defined by super class and the members of its own.

we can inherits one class by using extends keyword followed by the class name which want to inherit the properties.

syntax:

class subclass-name extends superclass-name

{

// body of class

}

// A simple example of inheritance.
// Create a superclass.
class A 
{
 int i, j;
 void showij() 
 {
 System.out.println("i and j: " + i + " " + j);
 }
}

// Create a subclass by extending class A.
class B extends A 
{
 int k;
 void showk()
 {
 System.out.println("k: " + k);
 }
  void sum() 
  {
 System.out.println("i+j+k: " + (i+j+k));
 }
}

public class SimpleInheritance 
{
 public static void main(String args []) 
 {
 A superOb = new A();
 B subOb = new B();

 // The superclass may be used by itself.
 superOb.i = 10;
 superOb.j = 20;

 System.out.println("Contents of superOb: ");
 superOb.showij();
 System.out.println();

 /* The subclass has access to all public members of
 its superclass. */
 subOb.i = 15;
 subOb.j = 18;
 subOb.k = 19;

 System.out.println("Contents of subOb: ");
 subOb.showij();
 subOb.showk();
    System.out.println();

 System.out.println("Sum of i, j and k in subOb:");
 subOb.sum();
 }
}

Output:

Contents of superOb:
i and j: 10 20

Contents of subOb:
i and j: 15 18
k: 19

Sum of i, j and k in subOb:
i+j+k: 52

Member Access

Although a subclass includes all the members of super class, but it will not inherit the members who declared as private.

//example for private members
class A 
{
 int i; // public by default
 private int j; // private to A

 void setij(int x, int y) 
 {
 i = x;
 j = y;
 }
}

// A's j is not accessible here.
class B extends A 
{
 int total;
 void sum() 
 {
 total = i + j; // ERROR, j is not accessible here
 }
}

public class Access 
{
 public static void main(String args[]) 
 {
 B subOb = new B();
 subOb.setij(10, 12);
 subOb.sum();
 System.out.println("Total is " + subOb.total);
 }
}

It will not execute, it will shows an error like total = i + j; // ERROR, j is not accessible here

Practical Example

// This program uses inheritance to extend Box.
class Box 
{
 double width;
 double height;
 double depth;

 // construct clone of an 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;
 }
}

// Here, Box is extended to include weight.
class BoxWeight extends Box 
{
  double weight; // weight of box

 // constructor for BoxWeight
 BoxWeight(double w, double h, double d, double m)
 {
 width = w;
 height = h;
 depth = d;
 weight = m;
 }
}

public class DemoBoxWeight 
{
 public static void main(String args[])
 {
 BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
 double vol;
 vol = mybox1.volume();
 System.out.println("Volume of mybox1 is " + vol);
 System.out.println("Weight of mybox1 is " + mybox1.weight);
  }
}

Output:

Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3

likewise we can create ColorBox inheriting Box with color as extra member.

Super class variable reference to subclass object

We can assign a reference of super class to a subclass derived from that super class.

public 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);
 System.out.println();

 // 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);
 }
}

Output:

Volume of weightbox is 105.0
Weight of weightbox is 8.37

Volume of plainbox is 105.0

when we assigning the reference of subclass to super class, it can access the members of only super class

Ex: it can,t access the weight variable even the subclass have that variable.

Because we are assigning it to super class it don't know what are the members in sub class.

*** we can't assign reference of super class to subclass as sub class is derived from super class only.

Using super

As we see we are using the members of super class in to sub class directly while initializing them as they are declared as default.

But for encapsulation property we have to declare members as private then we can't access directly there we have to use super which implicitly initializes the members of subclass through super class.

super has two general forms.

  1. To call super class constructor .
  2. To access the members of super class which are hidden from subclass (private)

1.

// A complete implementation of BoxWeight.
class Box 
{
 private double width;
 private double height;
 private double 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
 }

 // compute and return volume
 double volume() 
 {
 return width * height * depth;
 }
 }

// BoxWeight now fully implements all constructors.
class BoxWeight extends Box 
{
 double weight; // weight of box


   // constructor when all parameters are specified
 BoxWeight(double w, double h, double d, double m) 
 {
   super(w, h, d); // call superclass constructor
 weight = m;
 }

  // default constructor
 BoxWeight() 
 {
 super(); //default constructor
 weight = -1;
 }

}
public class DemoSuper 
{
 public static void main(String args[]) {
 BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);

 BoxWeight mybox3 = new BoxWeight(); // default

 double vol;
 vol = mybox1.volume();
 System.out.println("Volume of mybox1 is " + vol);
 System.out.println("Weight of mybox1 is " + mybox1.weight);
 System.out.println();

 vol = mybox3.volume();
 System.out.println("Volume of mybox3 is " + vol);
 System.out.println("Weight of mybox3 is " + mybox3.weight);
 System.out.println();

 }
}

Output:

Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3

Volume of mybox3 is -1.0
Weight of mybox3 is -1.0

In Parameterized constructor we uses super(w,h,d) to access the width , height, depth private members in the super class.

We can use super() function as many forms as there will be so many constructors of super class depends on the argument list it will execute the corresponding constructor.

2.

// Using super to overcome name hiding.
class A 
{
 int i; //default member
}

// Create a subclass by extending class A.
class B extends A 
{
 int i; // this i hides the i in A
 B(int a, int b) {
 super.i = a; // i in A as it is default that's why we can access
 i = b; // i in B
 }
 void show() 
 {
 System.out.println("i in superclass: " + super.i);
 System.out.println("i in subclass: " + i);
 }
}

public class UseSuper 
{
 public static void main(String args[])
 {
 B subOb = new B(1, 2);
 subOb.show();
 }
}

Output:

i in superclass: 1
i in subclass: 2

In this second form of super we can access the members of super class as this operator means which tells that this is the super class member (instance variables or methods).

It is useful when there we uses members with same names then to access super class member we have to use super.member to access members which are not declared as private.

results matching ""

    No results matching ""