Saturday 30 July 2016

Inheritance in Java

Inheritance in Java

Inheritance is one of the oops concepts in java.inheritance is concept of getting properties of one class object to another class object. 
Inheritance represents the IS-A relationship,also known as parent-child relationship.

Example of Inheritance

public class B {
  void f1(){}  
}
public class A extends B {
  void f2(){}
}
void main(String[] args) {
  A a =  new A();
  a.f2();
}

Why java doesn't support multiple Inheritance ?

See Below Code:

class A
{
      void test()
{
          System.out.println("test() method");
      }
}
class B
{
         void test()
{
            System.out.println("test() method");
         }
}
Suppose if Java allows multiple inheritance like this,
class C extends A, B
{
}

A and B test() methods are inheriting to C class.

So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity.


Reference: Inheritance in Java

No comments:

Post a Comment