Monday, 30 September 2013

Multilevel and Multiple Inheritance - Basic Java

Java support both multilevel and multiple inheritance.

MultiLevel Inheritance: This you can achieve by having super class and sub class then one more sub class for recently created subclass. For instance

class A{
}
class B extends A{
}
class C extends B{
}
class D extends C{
}

Multiple Inheritance: Directly not supported by java but  this can be achieved by using Interface concept in Java. For instance

interface Car {
//abstract methods and constant only
}

interface Engine{
//abstract methods and constant only
}

interface Break{
//abstract methods and constant only
}

class MyCar implements Car, Engine, Break{
   //Agreeing to implement all the methods containing in implemented interfaces
}