Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write about Types of inheritance with example. #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 180 additions & 19 deletions java/oops/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,9 @@ Inheritance is a process of acquiring parent properties and behaviour by a child

### Type of Inheritance

* Single inheritance
## 1. Single Inheritance

When a child inherits properties from a single parent

* Multiple inheritance

When a child inherits its properties from two or more parents.

* Hierarchical inheritance

When multiple children inherit properties from a parent

* Multilevel inheritance

When a parent properties are inherited by a child which again get interited by another child. For example, B is inherited from A and C is inherited from B.

A -> B -> C
* Hybrid inheritance

Hybrid inheritance is a combination or two or more inheritance types.
When a child inherits properties from a single parent

Below is an example of single inheritance

Expand All @@ -48,5 +31,183 @@ class iPhone extends Apple {
}
}
```

## 2. Hierarchical inheritance

When multiple children inherit properties from a parent

Below is an example of hierarchical inheritance

```java

class A {
public void print_A()
{
System.out.println("Class A");
}
}

class B extends A {
public void print_B()
{
System.out.println("Class B");
}
}

class C extends A {
public void print_C()
{
System.out.println("Class C");
}
}

class D extends A {
public void print_D()
{
System.out.println("Class D");
}
}

public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();
obj_C.print_A();
obj_C.print_C();

D obj_D = new D();
obj_D.print_A();
obj_D.print_D();
}
}

```

## 3. Multilevel inheritance

When a parent properties are inherited by a child which again get interited by another child. For example, B is inherited from A and C is inherited from B.

A -> B -> C

```java

class A {
public void printA()
{
System.out.println("Class A");
}
}

class two extends one {
public void printB()
{
System.out.println("Class B");
}
}

class three extends two {
public void printC()
{
System.out.println("Class C");
}
}

// Drived class
public class Main {
public static void main(String[] args)
{
three g = new three();
g.printA();
g.printB();
g.printC();
}
}

```

## 4. Multiple inheritance

When a child inherits its properties from two or more parents. It can be done by the help of Interface.

Below is an example of multiple inheritance.

```java

interface one {
public void printA();
}

interface two {
public void printB();
}

interface three extends one, two {
public void printC();
}
class child implements three {
public void printA()
{
System.out.println("Interface");
}

public void printB()
{
System.out.println("Class B");
}

public void printC()
{
System.out.println("Class C");
}
}

public class Main {
public static void main(String[] args)
{
child c = new child();
c.printA();
c.printB();
c.printC();
}
}

```


## 5. Hybrid inheritance

Hybrid inheritance is a combination or two or more inheritance types.

Below is the example of hybrid inheritance.

```java

public class ClassA
{

}
public class ClassB extends ClassA
{

}
public class ClassC extends ClassA
{

}
public class ClassD extends ClassB
{

}

```



## Practice with more examples [here](https://onecompiler.com/java)