0% found this document useful (0 votes)
4 views6 pages

Lec16 Inheritance-2

Uploaded by

m4vinayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Lec16 Inheritance-2

Uploaded by

m4vinayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

There the five types of inheritance as below

1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hybrid inheritance
5. Hierarchical inheritance
1. Simple or Single inheritance
In this only one super class and only one sub class called as single level inheritance

package com.single.inheritance;

public class A {

void m1() {
System.out.println("super class- m1 ()
method");
}
}

package com.single.inheritance;

public class B extends A {

void m2() {
System.out.println("sub class - m2() method");
}

package com.single.inheritance;

public class TestMain {

public static void main(String[] args) {

B b = new B();
b.m1();
b.m2();
}
}

2. Multilevel inheritance
It has only one base class and multiple derived class called as multilevel. Or it refers
to the concept of one class extending (Or inherits) more than one base class.
package com.multilevel.inheritance;

public class A {

void m1() {
System.out.println("Class A- m1 () method");
}
}

package com.multilevel.inheritance;

public class B extends A{

void m2() {
System.out.println("Class B- m2 method");
}
}

package com.multilevel.inheritance;

public class C extends B {

void m3() {
System.out.println("Class c- m3 () method");
}

public static void main(String[] args) {

C c= new C();
c.m1();
c.m2();
c.m3();
}
}

3. Multiple inheritance
One class has many super classes called as multiple inheritance.
Why multiple inheritance not supported in java in case of classes?

Class base has test () method and class derived has also test () method. Class test extends
Base, Derived, which test method It will called, so it create the ambiguity so that’s why
multiple inheritance does not supports in java.

package com.multiple.inheritance;

public class A {

void m1() {

}
}
package com.multiple.inheritance;

public class B {

void m1() {

}
}

package com.multiple.inheritance;

class C extends A,B {

public static void main(String[] args) {

C c= new C();
c.m1();
}
}

Note- it will get the compile time error.

4. Hierarchical inheritance
One class is inherited by many sub classes called as.

package com.hierachical.inheritance;

public class A {

void m1() {
System.out.println("Class A- m1 () method");
}
}

package com.hierachical.inheritance;

public class B extends A {

void m2() {
System.out.println("Class B- m2() method");
}
}

package com.hierachical.inheritance;

public class C extends A{

void m3() {
System.out.println("Class c m3 method");
}
}

package com.hierachical.inheritance;

public class D {

public static void main(String[] args) {

B b = new B();
C c = new C();
b.m1();
b.m2();
c.m3();
}
}

5. Hybrid inheritance
It is the combination of single and multiple inheritance. So it is not allowed in
java.

Aggregation (Has Relationship)-


If a class has an entity reference, it is known as Aggregation. Aggregation represents HAS-A
relationship.

Example-
Class Employee{
Int id;
String firstName;
String lastName;
Address address;
}

Class Address {
String streetNo;
String city;
String state;
String country;
}

You might also like