Inharitance


Main Keywords:

Parent Class(Base Class)(super class)

Child Class(Drive Class)(sub class)

Extends, Super

Inheritance Type:

  • Single inheritance.(1 parent 1 child)
  • Multi-level inheritance.(grand parent-parent-child) 
  • Multiple inheritance.(1 child many parent) //Not supported by java
  • Hierarchical Inheritance.(1 parent many child)

Practice Program:

Question of Single inheritance:

Create a base class Animal having only one function eat in which simple print("Aminaal is eating").
Then a derived class Dog also has one function bark in which simple print("Dog is barking").
Create a main class in which make an object of dog class and call both functions base and derived class
Create a class diagram as well as identify which type of inheritance it is?

Question of Multi-Level Inheritance:

write a program that creates a parent class Vehicle having one function start() in which simple write vehicle is starting. Now create a child class of Vehicle Car having function drive() in which a simple print car is being driven. Create a child class SportCar of the car having function acceleration Sports car is accelerating.
Now create a main class a made object of SportCar and call all the functions
Create a class diagram as well as identify which type of inheritance it is?

Question of Hierarchical Inheritance:

write a program that has 1 base class Shape having function draw simple print init "Drawing a shape" and 2 derived class Circle and Rectangle having the same function draw in which write init "Drawing a circle and drawing a rectangle " now create circle object and call main function + call Rectangle class and call the main function in main.
Create a class diagram as well as identify which type of inheritance it is?

Question of Multiple Inheritance:

write a program that creates two classes P1 and P2 having the same function fun in which simply print parent1 and parent2. Now create one child class name c1 that extends both P1 and P2.In Main create an object of the child class and call the fun function(Remember Multiple Inheritance is not supported in Java)


Task 1.                                            

Consider a base class person which has some common attributes such as :

 

Name: Id: Address: Age: Gender: Phone_No:

 

A default constructor to initialize the data members with default values. A parameterize constructor which accepts the values of these data members as parameter and initialize these data members with those values: Also setter and getter functions for each data member to set values.(values are input from the user through scanner ).

Write an displayinfo() method to get return tostring from.

 

Now make two child or sub-classes:

Doctor class which inherit all the members from the parent class(name , id , address, age, gender ,phone_no) and also have its own data members :

 Duty timing: hire_date: Salary:

 

Make appropriate getter and setter function and constructor for this class .

 

Write an displayinfo() method to get return tostring from display to display all these properties related to doctor.

 

Child class patient which also inherit all the properties from base class like doctor class .Also have some own properties such as:

Disease type: Appointment time:

make appropriate getter and setter function and constructor for this class.

Write an displayinfo() method to get return tostring from display to display all these properties related to patient.

Write a method getAppointmentTime(string time) to return an appointment time set by doctor.

At the last make a class name hospital with main method and in main method you have to create the object of patient and doctor and call the displayinfo() method to afterinput for user and getAppointmentTime() by patient class.

Task 2:

Imagine a publishing company that markets both book and audiocassette versions of its works.

Create a class publication that stores the title (a string) and price (type float) of a publication.

From this class inherit two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float).

user and set it, and a getter function to get its data.

Write a main () program to test the book and tape classes by creating instances of them and then displaying the data using toString().

Task 3:

// Java Program to Illustrate Unsupportance of

// Multiple Inheritance

 

// Importing input output classes

import java.io.*;

 

// Class 1

// First Parent class

class Parent1 {

 

// Method inside first parent class

void fun() {

 

            // Print statement if this method is called

            System.out.println("Parent1");

}

}

 

// Class 2

// Second Parent Class

class Parent2 {

 

// Method inside first parent class

void fun() {

 

            // Print statement if this method is called

            System.out.println("Parent2");

}

}

 

// Class 3

// Trying to be child of both the classes

class Test extends Parent1, Parent2 {

 

// Main driver method

public static void main(String args[]) {

 

            // Creating object of class in main() method

            Test t = new Test();

 

            // Trying to call above functions of class where

            // Error is thrown as this class is inheriting

            // multiple classes

            t.fun();

}

}


Important Points:

  1. If the class is final then it is not Inherited.
  2. If the function is final then the class is inherited but the function is not @override.
  3. The subclass inherits all the non-private members (protected and public members).
  4. constructors are not inherited to inherited but we can call them in subclass using the super keyword.
  5. Inheritance is a key factor in achieving polymorphism.
  6. Multiple Inheritance is not supported due to Diamond Problem
  7. Every class in Java is a subclass of the Object class(Implicity Extends)