Inheritance:
- The concept of getting properties of one class object to another class object is known as inheritance.
- Here properties means variable and methods.
Types of Inheritance:
- Multiple inheritance.
- Multilevel inheritance.
Multiple inheritance:
- The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
- Java Doesn't Support multiple Inheritance.
Diamond problem:
- In multiple inheritance there is every chance of multiple properties of multiple objects with the same name available to the sub class object with same priorities leads for the ambiguity.
- //Multiple inheritance program
- Class A{
- }
- Class B extends A{
- public void show(){
- }
- }
- Class C extends A{
- public void show(){
- }
- }
- Class D extends B,C{ // not supported by java leads to syntax error.
- }
- We have two classes B and c which are inheriting A class properties.
- Here Class D inheriting B class and C class So properties present in those classes will be available in java.
- But both classes are in same level with same priority.
- If we want to use show() method that leads to ambiguity
- This is called diamond problem.
- Because of multiple inheritance there is chance of the root object getting created more than once.
- Always the root object i.e object of object class hast to be created only once.
- Because of above mentioned reasons multiple inheritance would not be supported by java.
- Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.
Is Java supports multiple inheritance using interfaces?
- Before that we need to know about interfaces.
- Interfaces having fully abstract functionality.
- Means methods in interfaces by default public abstract methods so we need to implement these methods in classes which are extending this interface.
- /Multiple inheritance program
- interface A{
- public void show();
- }
- interface B{
- public void display();
- }
- Class C Implements A,B{
- }
- Here it seems we are achieving multiple inheritance by using interfaces. but we are not.
- Syntactically it seems to multiple inheritance but the actual implementation of multiple inheritance is not there. how can i say that? let me clear
Difference between interfaces and inheritance: in multiple inheritance
- Inheritance means getting the properties from one class object to another class object.
- Means if any class extending another class then the super class methods can be used in sub classes happily.
- But in interfaces the methods in interfaces are fully abstract so we need to provide functionality in our extended class and use it .seems both are opposite right? yes.
- That is the reason we can say interfaces are only supports syntactical multiple inheritance which is not implementation of multiple inheritance.
Inheritance is like debit and interface is like credit but interface has its own importance in other concepts like server side programming
What is the major change in JAVA 8 regarding interfaces:
- Yes from java 8 we can add static methods and defaults methods in interfaces by this we can achieve multiple inheritance.
- Java 8 interface static and default methods
Thank you.im am fresher in programing field.and little nerves to learn programs.and i saw this website.its all about easy to learn java.
ReplyDeleteyeah .. vikky your correct !!!
DeleteWow :o Awesome !!!
DeleteExplanation ,I never seen any website so far rather than it ...it cleared my doubts whichever I was getting on OOPS as well Collections ...
Really I would completely appreciate from my deep heart