Skip to main content

 Inheritance

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another(parent). It is an important part of OOP. The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

Syntax

class Super {

   .....

....

}

class Sub extends Super {

   .....

   .....

}

Types of Inheritance

Java supports the following four types of inheritance:

       Single Inheritance

       Multi-level Inheritance

       Hierarchical Inheritance

       Hybrid Inheritance

Single Inheritance

In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as simple inheritance.

Example:
class Employee {
void display() {
System.out.println("I am an Employee");
}
}
class Executive extends Employee {
void show() {
System.out.println("I am an Executive");
}
}
public class Main {
public static void main(String[] args) {
Executive e = new Executive();
e.display(); // from Employee
e.show(); // from Executive
}
}

Multi-level Inheritance

In multi-level inheritance, a class is derived from a class which is also derived from another class is called multi-level inheritance. In simple words, we can say that a class that has more than one parent class is called multi-level inheritance.

Example:

class Student {          // Super class

void studentInfo() {

        System.out.println("This is Student class");

    }

}

class Marks extends Student {   // Intermediate subclass

    void marksInfo() {

        System.out.println("This is Marks class");

    }

}

class Sports extends Marks {    // Derived class

    void sportsInfo() {

        System.out.println("This is Sports class");

    }

}

public class Main {

    public static void main(String[] args) {

        Sports s = new Sports();

        s.studentInfo();  // from Student class

        s.marksInfo();    // from Marks class

        s.sportsInfo();   // from Sports class

    }

}

Hierarchical Inheritance

If a number of classes arederived from a single base class, it is called hierarchical inheritance.


// Superclass class Student { void display() { System.out.println("I am a Student"); } } // Subclass 1 class Science extends Student { void show() { System.out.println("I am a Science Student"); } } // Subclass 2 class Commerce extends Student { void show() { System.out.println("I am a Commerce Student"); } } // Subclass 3 class Arts extends Student { void show() { System.out.println("I am an Arts Student"); } } // Main class public class Main { public static void main(String[] args) { Science s = new Science(); Commerce c = new Commerce(); Arts a = new Arts(); s.display(); s.show(); c.display(); c.show(); a.display(); a.show(); } }

Hybrid Inheritance

Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.

Example:
class GrandFather { void showGrandFather() { System.out.println("I am GrandFather."); } } // Father inherits GrandFather (Single Inheritance) class Father extends GrandFather { void showFather() { System.out.println("I am Father."); } } // Hierarchical Inheritance: Father → Son and Father → Daughter class Son extends Father { void showSon() { System.out.println("I am Son."); } } class Daughter extends Father { void showDaughter() { System.out.println("I am Daughter."); } } // Hybrid Inheritance is demonstrated by combination of above relations

public class FamilyInheritance {
public static void main(String[] args) { System.out.println("--- Son's Family ---"); Son s = new Son(); s.showGrandFather(); // Inherited from GrandFather s.showFather(); // Inherited from Father s.showSon(); // Own method System.out.println("\n--- Daughter's Family ---"); Daughter d = new Daughter(); d.showGrandFather(); // Inherited from GrandFather d.showFather(); // Inherited from Father d.showDaughter(); // Own method } }

Comments

Popular posts from this blog

Function of OS 1. Process Management The operating system helps in running many programs at the same time. It keeps track of each running program (called a process), decides which one should run next, and stops or starts them as needed. It makes sure that all the programs get a fair chance to use the CPU.   2. Memory Management The OS manages the computer's memory (RAM). It decides which program will use how much memory and keeps track of it. When a program is closed, it frees up the memory so that other programs can use it. This helps the computer run smoothly without crashing.   3. File System Management The operating system helps us to create, save, open, and delete files. It organizes files in folders and keeps them safe. It also controls who can open or edit a file to protect our data.   4. Device Management The OS controls all the input and output devices like the keyboard, mouse, printer, and monitor. It tells the devices what to do and makes su...

UNIT 3 Array in C#

  Array in C# An array is a collection of variables of the same data type , stored in contiguous memory locations , and accessed using a common name and index . Each item in an array is called an element , and the index of arrays in C# starts from 0 .     Key Points About Arrays in C#: 1.       Elements are stored in contiguous memory locations. 2.       Index starts from 0. So, for an array of size 5, valid indexes are 0 to 4. 3.       Arrays are reference types and are allocated on the heap. 4.       C# array is an object of base type System.Array . 5.       Array elements can be of any type , including another array (array of arrays). 6.       Jagged array (array of arrays) elements are reference types and are initialized to null . 7.       Arrays can be single-dimensi...

Unit 2 Java

Data Types A data type defines the kind of data a variable can store, the operations that can be performed on it, and the amount of memory allocated. Java Primitive Data Types Data Type Size Range (Approx.) Example Byte 1 byte -128 to 127 byte a = 100; short 2 bytes -32,768 to 32,767 short s = 1000; Int 4 bytes -2,147,483,648 to 2,147,483,647 int num = 50000; Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 long l = 100000L; float 4 bytes ~6–7 decimal digits float f = 5.75f; double 8 bytes ~15 decimal digits double d = 19.99; Char 2 bytes Single Unicode character char c = 'A'; boolean 1 bit* tru...