2. Inheritance
Inheritance is the mechanism of deriving a new class (ca
lled derived class) from an old one (called base class).
Inheritance is a powerful code reuse mechanism where
a derived class inherits all/some of the description of th
e base class.
A class whose properties are being acquired by some
other class or a class that is inherited is called the super
class or base class or parent class.
The class that acquires the properties of some other
class or the class that does the inheriting is called the
subclass or derived class or child class.
A derived class inherits all/some of the properties from
base class.
A derived class can also inherit properties from more
than one class / from more than one level.
3. Inheritance
Reusability: Inheritance is a powerful code reuse mecha
nism where a derived class inherits all/some of the
properties of the base class. That is, one can derive a
new class (sub-class) from an existing class and add new
features to it without modifying its parent class. There
is no need to rewrite the parent class in order to inherit
it.
Transitive nature: If class A inherits properties from
another class B, then all subclasses of A will
automatically inherit properties from B. This property is
called the transitive nature of inheritance.
Note: A subclass defines only those features that are
unique to it.
4. Inheritance
Example, the class Student inherits from the
class Person. Then although Student is a person, the
reverse is not true. A Person need not be a Student. The
class Student has properties that it does not share with
class Person.
For instance, the Student has a marks-percentage, but
the Person does not have.
5. Inheritance using class hierarchies
Vehicles
Land Vehicle Water Vehicle
Base Class
Car Rickshaw
Ship
Boat
Derived Class
is a subtype of
14. Single Inheritance - Example
//Base class
class Person {
String name = "John";
int age =17;
String city = "Delhi";
public void show() {
System.out.println("Student inheriting properties from Person:n");
}
}
//child class
class Student extends Person {
// defining additional properties to child class
String Course = "Java";
int marks = 78;
}
15. Single Inheritance - Example
class Main{
public static void main(String args[]) {
Student obj = new Student();
obj.show();
System.out.println("Name of the student is: " + obj.name);
System.out.println("Age of the student is: " + obj.age);
System.out.println("Student lives in: " + obj.city);
System.out.println("Student learns from: " + obj.tutorial);
System.out.println("Marks obtained by the student is: " + obj.marks);
}
}
16. Multilevel Inheritance - Example
class Person {
public void show() {
System.out.println("Person
class");
}
}
class Student extends Person {
public void show1() {
System.out.println("I am a
Student inheriting the
properties of Person");
}
}
class EngineeringStudent extends Student {
public void show2(){
System.out.println("Engineering Student
inheriting properties from Student");
}
}
public class MultilevelDemo {
public static void main(String args[]) {
EngineeringStudent obj = new
EngineeringStudent();
obj.show();
obj.show1();
obj.show2();
}
}
17. Hierarchical Inheritance - Example
class Person
{
public void show()
{
System.out.println("I am a
Person");
}
}
//child class1
class Student extends Person
{
public void show1()
{
System.out.println("I am a
Student who is Person ");
}
}
//child class2
class Doctor extends Person{
public void show2() {
System.out.println("I am a Doctor who is a
Person");
}
}
public class HierarchicalInheritance{
public static void main(String args[]){
Student student = new Student();
Doctor doctor = new Doctor();
student.show();
student.show1();
doctor.show2();
}
}