Learn Inheritace in Java


Java Inheritance is an object oriented concept that enables deriving new class from an already existing class. To know the concept of inheritance clearly you must have the idea of class and its features like methods, data members, access controls, constructors, keywords this, super etc. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Inheritance is the mechanism through which we can derived classes from other classes. The derived class is called as child class or the subclass or  we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class. To derive a class in java the keyword extends is used.
For example a car class can inherit some properties from a General vehicle class. Here we find that the base class is the vehicle class and the subclass is the more specific car class.
A subclass must use the extends clause to derive from a super class which must be written in the header of the subclass definition. The subclass inherits members of the superclass and hence promotes code reuse. The subclass itself can add its own new behavior and properties. The java.lang.Object class is always at the top of any Class inheritance hierarchy.

Types of Java Inheritance are:
  • Single Inheritance – Single Inheritance is a type of Java Inheritance in which the derived class will have only one super class or base class. Single Inheritance is implemented using Java Class.
  • Hierarchical Inheritance – Hierarchical Inheritance is a type of Java Inheritance in which one base class can have many derived classes inherited from it. Hierarchical Inheritance is implemented using Java Class.
  • Multilevel Inheritance – Multilevel Inheritance is a type of Java Inheritance in which the derived class can be derived further by another derived class to any number of levels. Multilevel Inheritance is implemented using Java Class.
  • Multiple Inheritance – Multiple Inheritance is a type of Java Inheritance in which the derived class will have several super classes or base classes. Multiple Inheritance is implemented using Java Interfaces. 
Example: 

class Box {

 double width;
 double height;
 double depth;
 Box() {
 }
 Box(double w, double h, double d) {
  width = w;
  height = h;
  depth = d;
 }
 void getVolume() {
  System.out.println("Volume is : " + width * height * depth);
 }
}

public class MatchBox extends Box {

 double weight;
 MatchBox() {
 }
 MatchBox(double w, double h, double d, double m) {
  super(w, h, d);
  weight = m;
 }
 public static void main(String args[]) {
  MatchBox mb1 = new MatchBox(10, 10, 10, 10);
  mb1.getVolume();
  System.out.println("width of MatchBox 1 is " + mb1.width);
  System.out.println("height of MatchBox 1 is " + mb1.height);
  System.out.println("depth of MatchBox 1 is " + mb1.depth);
  System.out.println("weight of MatchBox 1 is " + mb1.weight);
 }
}
Output
Volume is : 1000.0
width of MatchBox 1 is 10.0
height of MatchBox 1 is 10.0
depth of MatchBox 1 is 10.0
weight of MatchBox 1 is 10.0


Reactions:

0 comments:

Post a Comment