Infolinks In Text Ads

Inheritance in Java

In object-oriented programming, inheritance enables new objects to take on the properties
of existing objects.
A class that is used as the basis for inheritance is called a superclass or base class.
A class that inherits from a superclass is called a subclass or derived class.
The terms parent class and child class are also acceptable terms to use respectively.
 A child inherits visible properties and methods from its parent while adding additional
 properties and methods of its own.
  • Father –Child Relationship.
  • Inheritance relationships organize classes into generalization-specialization (superclass-subclass) hierarchies; they provide a basic re-use mechanism for sharing attributes and operations.
  • Know as “is-a” relationship
Example Code:




package com.aurangzeb.utk;

public class PaymentMethod {
     
}



package com.aurangzeb.utk;

public class Cash extends PaymentMethod {
     
}


package com.aurangzeb.utk;

public class Check extends PaymentMethod {
     
}


Class Diagram:


Example Code:


package aurangzeb.utk.com;

public class Box {
      double width, height, depth;

      Box() {// initializes to some default values
            width = 10;
            height = 10;
            depth = 10;
      }

      Box(double w, double h, double d) {// specifies dimensions
            width = w;
            height = h;
            depth = d;
      }

      Box(Box ob) {// constructs clone of an object
            width = ob.width;
            height = ob.height;
            depth = ob.depth;
      }

      Box(double len) {// for creating cube
            this(len, len, len);
      }

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





package aurangzeb.utk.com;

public class BoxColor extends Box {
      int color;

      BoxColor(double w, double h, double d, int c) {
            width = w;
            height = h;
            depth = d;
            color = c;
      }
}



package aurangzeb.utk.com;

public class BoxWeight extends Box {
      double weight;

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




package aurangzeb.utk.com;

public class BoxInheritanceDemo {
                 
      public static void main(String[] args) {
            BoxColor boxC = new BoxColor(10, 20, 15, 500);
            BoxWeight boxW = new BoxWeight(4, 7, 9, 35.5);
            System.out.println("Color of boxC is " + boxC.color);
            System.out.println("Volume of boxC is" + boxC.volume());
            System.out.println("Weight of boxW is " + boxW.weight);
            System.out.println("Volume of boxW is" + boxW.volume());
      }
     
     
}

Class Diagram


  • BoxColr is-a Box
  • BoxWeight  is-a Box
Memory Layout

  • Each object of a child class has an object of its parent class as its part!
  • This object of parent‟s class can be referenced with another hidden reference variable : „super‟
Supper  keyword
  • Super is a special keyword which has two-fold purpose
  • To call the constructors of base class super (), super (xyz)
  • To access member variables and methods inherited from base
  • In case of calling constructor of base Only a constructor of a child class can call constructor of the base class using ‘super(…)’
  • Call to ‘super(…)’ must be the very first line in the child’s constructor
  • If you don't call super(xyz)[or this(xyz)] explicitly as the first line in a constructor, Java will automatically insert a call to the default no-argument superclassconstructor for you super();

Newer Post Older Post

Leave a Reply

Powered by Blogger.