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:
Question of Multi-Level Inheritance:
Question of Hierarchical Inheritance:
Question of Multiple Inheritance:
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:
- If the class is final then it is not Inherited.
- If the function is final then the class is inherited but the function is not @override.
- The subclass inherits all the non-private members (protected and public members).
- constructors are not inherited to inherited but we can call them in subclass using the super keyword.
- Inheritance is a key factor in achieving polymorphism.
- Multiple Inheritance is not supported due to Diamond Problem
- Every class in Java is a subclass of the Object class(Implicity Extends)