answersLogoWhite

0

Explain forms of inheritance in java with examples?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

Types of Inheritance:

They are:

1. Single Inheritance

A Scenario where one class is inheriting/extending the behavior of just one super class.

Ex: public class Ferrari extends Car {…}

2. Multilevel Inheritance

A Scenario where one class is inheriting/extending the bahavior of another class which in turn is inheriting behavior from yet another class.

Ex: public class Automobile {…}

Public class Car extends Automobile {…}

Public class Ferrari extends Car {…}

This multilevel inheritance actually has no limitations on the number of levels it can go. So as far as java goes, it is limitless. But for maintenance and ease of use sakes it is better to keep the inheritance levels to a single digit number.

3. Multiple Inheritance

Actually, java does not support multiple inheritance. You can achieve partial multiple inheritance using interfaces but java is not like C or C++ where you can do direct multiple inheritance. However, you can achieve partial multiple inheritance with the help of interfaces.

Ex: public class FerrariF12011 extends Ferrari implements Car, Automobile {…}

And this is under the assumption that Car and Automobile are interfaces.

Here if you see, though you don't inherit concrete code from the Car or the Automobile interface, you do inherit skeleton methods that determine the way your class eventually behaves and hence this can be considered partial Multiple Inheritance.

If you are curious asking me why doesn't java allow multiple inheritance, think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below:

Public class FerrariF12011 extends Ferrari, Car, Automobile {…}

And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature.

4. Hybrid Inheritance

This is the scenario wherein your java code exhibits traits of more than one of the inheritance types explained above. Going with the example above again:

Public class FerrariF12011 extends Ferrari implements Car, Automobile {…}

The above is a combination of both single and multiple inheritance.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain forms of inheritance in java with examples?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the examples of Java script?

validation of forms, date & time.


Specialization form of inheritance?

Inheritance refers to the concept by which the features from one class can be extended/made available in other classes. Java supports 3 forms of inheritance * Single Inheritance * Multiple Inheritance * Multilevel Inheritance (Can be implemented using interfaces)


Specification forms of inheritance in java?

single level inheritance eg ( class B extends Class A) Multilevel inheritance eg( class C extends class B and class B extends class A) multiple inheritance Class C inherits Class A features as well as Class B featues.This type of inheritance is not allowed in JAVA.


What does java not support?

Java does not support multiple inheritance.......


What are the features of core java?

Core java refers to the core or basic concepts of the Java programming language. Things like encapsulation, inheritance, multi-threading, exception handling and other basic feature of java that comes as part of the Java standard edition forms Core Java


What is the definition of Core Java?

Core java refers to the core or basic concepts of the Java programming language. Things like encapsulation, inheritance, multi-threading, exception handling and other basic feature of java that comes as part of the Java standard edition forms Core Java


What is ambiguity in multiple inheritance?

Java does not support multiple inheritance


What are the features of the core?

Core java refers to the core or basic concepts of the Java programming language. Things like encapsulation, inheritance, multi-threading, exception handling and other basic feature of java that comes as part of the Java standard edition forms Core Java


What kind of inheritance is not allowed in java?

Java does not allow the multiple inheritance of concrete classes, though it does allow a "hybrid" inheritance of one concrete class and multiple interfaces.


What is problem in multiple inheritance?

Java does not support direct multiple Inheritance. Harder to implement, not every language support it: C++ does, Java does not.


Give the structure of multiple inheritance?

Java does not support multiple inheritance. It is done with the help of interfaces in java. a class can implement n number of interfaces, thus showing multiple inheritance. but a class cannot extend multiple classes in java.


What is public inheritance in java?

Inheritance is a Java feature by which we can reuse code and programming logic from one class in another class. We implement Inheritance using the extends keyword.Ex: public class Ferrari extends Car {…}Here the Ferrari Class will extend features from the Car Class.This is Inheritance. The different types of Inheritance are:Single InheritanceMulti-Level InheritanceMultiple Inheritance (Java supports only Partial Multiple Inheritance) andHybrid Inheritance