Day31: Inheritance, Super()

 Inheritance

Inheritance is one of the core principles of Object Oriented Programming. As the name indicates to inherit means to acquire features of existing entity in a newly created entity. Just like a child inherits the features of his/her parents.

Similarly, if while designing a class in Java, a programmer wants then he can use the features(instance variable and Methods) of an existing class into his own class. But to do this the programmer will have to apply the concept of INHERITANCE. In java, the class will gets inherited is called as SUPER CLASS and the class which inherits is called as SUB CLASS. Thus via inheritance member of super class can also be accessed through the object of sub class.

Benefits of Inheritance

  1. The major benefit of applying inheritance is code reusability. This is because the sub class programmer is not required to redesign those methods or java again which already have been provided by the super class. This saves lor of time and effort on the part of sub class programmer.
  2. Another benefit of inheritance is maintainability. This is because if we have apply inheritance and we want to add a new common feature to all the classes.

Syntax of Inheriting a class

class <class_name>
{
    ----------
    ----------
}

class <class_name> extends <class_name>
{
    ---------
    ---------
}

Types of inheritance in OOPs

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
But Java support only 3 types of INHERITANCE i.e., Single, Multilevel, Hierarchical Inheritance.
Java strongly oppose Multiple & Hybrid inheritance.

Q) Why java does not support multiple Or Hybrid Inheritance.

Multiple / Hybrid inheritance allows feature of two different classes to be inherited in the third class. Due to this ambiguity can arise if both the parent class contain method with same prototype. The designer of java language wanted to keep Java as simple as possible and so to avoid ambiguity problem they decided not support multiple/hybrid inheritance.

Although to a certain extend java provides multiple inheritance support using another feature called inheritance.
package inhdemo;

public class Emp
{
    private String name;
    private double salary;

    public void setData(String name, double salary){
        this.name = name;
        this.salary = salary;

    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }
}

File Name : Manager.java

package inhdemo;

public class Manager extends Emp
{
    private double bonus;

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    public double getTotalIncome(){
        double income;
        income = getSalary() + bonus;
        return income;
    }
}


File Name: UseManager.java
package inhdemo;

public class UseManager
{
    public static void main(String[] args) {
        Manager boss = new Manager();
        boss.setData("Rakesh", 50000.0);
        boss.setBonus(10000.0);
        System.out.println("Manager's Name "+boss.getName());
        System.out.println("Manager's Income "+boss.getTotalIncome());

    }
}


Using the keyword "Super"

In java we've a special keyword called 'super' which is use by child class for explicitly referring.
For explicitly referring member of parent class. Using super becomes compulsory in two situation:
  1. For calling parent class constructor from child class.
  2. For handling method overriding.

Behaviour of Constructor In Inheritance

1) In Java whenever we create an object of CHILD class, then Java automatically calls the constructor of PARENT class first followed by the constructor of CHILD class.

2) If the CONSTRUCTOR of parent class is NOT PARAMETERIZED then programmer has no role to play in it. Since everything is automatically handled by Java  itself.

3) But if the PARENT CLASS has only parameterized constructor then Java expects 2 things from the programmer:
a. The Programmer must define a CONSTRUCTOR in CHILD CLASS.
b. From that constructor the programmer must call the CONSTRUCTOR of PARENT CLASS.

And for this programmer will have to use the keyword "super".
Moreover the line used for calling the PARENT CLASS constructor must be the first line of the child class constructor body.

CALLING OF NON-PARAMETERIZED CONSTRUCTOR OF PARENT CLASS

class A
{
public A()
{
System.out.println("Constructor of A Called...");
}
}
class B extends A
{
public B()
{
System.out.println("Constructor of B Called...");
}
}
class UseB
{
public static void main(String [ ] args)
{
B obj = new B();
}
}

Output : Shown in figure


CALLING OF PARAMETERIZED CONSTRUCTOR OF PARENT CLASS

class A
{
public A(int i) //Parameterized
{
System.out.println("Constructor of A Called...");
}
}
class B extends A
{
public B()
{
    super(15);
            System.out.println("Constructor of B Called...");
}
}
class UseB
{
public static void main(String [ ] args)
{
B obj = new B();
}
}

Q) What happen in this code, Should it Compile and Run

class A
{
public A(int i)
{
this();
System.out.println("Single Parameterized constructor called...");
}
public A(int i, int j)
{
this(i);
System.out.println("Double Parameterized constructor called...");
}
public A()
{
System.out.println("Non-Parameterized Constructor Called...");
}
}

class B extends A
{
public B()
{
super(25,50); 
System.out.println("Constructor of B Called...");
}
}

class UseB
{
public static void main(String [ ] args)
{
B obj = new B();
}
}

Output: In the figure,



Example on Above Code

File name : Emp.java

package inhdemo;

public class Emp extends Object
{
private String name;
private double salary;

public Emp(String name, double salary){
this.name = name;
this.salary = salary;

}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}
}
File name : Manager.java

package inhdemo;

public class Manager extends Emp
{
private double bonus;

public Manager(String name, double salary, double bonus) {

super(name,salary);
this.bonus = bonus;
}

public double getTotalIncome(){
double income;
income = getSalary() + bonus;
return income;
}
}
File Name : UseEmp.java

package inhdemo;

public class UseManager
{
public static void main(String[] args) {
Manager boss = new Manager("Rakesh", 50000.0, 10000.0);
System.out.println("Manager's Name "+boss.getName());
System.out.println("Manager's Income "+boss.getTotalIncome());

}
}
Output: 
Manager's Name Rakesh
Manager's Income 60000.0


Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?