Showing posts with label multiple inheritance. Show all posts
Showing posts with label multiple inheritance. Show all posts

Friday, 21 February 2014

Abstract Class versus Interface

1. Interfaces support multiple inheritance, where as abstract classes are not.

2. All the fields declared in interface are static final, where as you can declare static, non static, final, non-final variables in abstract class. There is no restriction in abstract classes.

3. All the methods declared in interface are abstract by default. It is not necessary for an abstract class to have abstract methods.

4. Abstract class can have concrete methods, where as interface don't. (From Java8 onwards we can provide default behaviour to methods in interfaces)

5. Abstract class can have constructor, where as interface not.

6. Abstract classes are used to share the common behavior. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

7. An abstract class can implement the interface, it doesn't mean that it provides implementation for all the methods in the interface

Some Points to Remember
1. If I can simulate Abstract class as Interface, why java provides Interface?
because you can implement multiple interfaces, but can't extend multiple abstract classes. To support multiple inheritance, interfaces are necessary.


Abstract methods & Abstract classes                                                 Garbage Collection                                                 Home

Wednesday, 19 February 2014

Hybrid Inheritance

It combines two or more forms of inheritance.


Classes 'A' and 'B' form Single inheritance and
Classes 'B', 'C' and 'D' form Multiple inheritance.

Multiple Inheritance                                                 Multipath Inheritance                                                 Home

Multiple Inheritance

In multiple inheritance one class derived from more than one super class.

Java doesn't support multiple inheritance.

Why Java not supporting multiple inheritance ?
Lets say there is a method called print(), in the super classes A and B
class A{
 void print(){
  System.out.println(I am in A);
 }
}

class B{
 void print(){
  System.out.println(I am in B);
 }
}

class C extends A, B{
 public static void main(String args[]){
  print();
 }
}

Class “C” is calling the method print(), then Java run time has two options, one is in the class A and other is in class B, here is an ambiguity. Of course there are languages like C++ provides a way to solve this problem, but to make the Java language simple, Java doesn't support multiple inheritance directly.

Java supports multiple inheritance using interfaces. A Java class allowed to implement more than one interface.


Hierarchical Inheritance                                                 Hybrid Inheritance                                                 Home

Types of Inheritance

There are six types of inheritance in Object Oriented Programming.

Those are:

Inheritance                                                 Single Level Inheritance                                                 Home