Monday, November 11, 2013

Java Inheritance Example

class Parent {
    Parent(int a, int b) {
        int c = a + b;
        System.out.println("Sum=" + c);
    }
    void display() {
        System.out.println("Return Statement");
    }
}
class Child extends Parent {
    Child(int a, int b) {
        int c = a - b;
        System.out.println("Difference=" + c);
    }
}
public class InheritanceExample {
    public static void main(String args[]) {
        Child c = new Child(2, 1);
        c.display();
    }
}
 
 
class child extends parent
{
    child(int a,int b)
    {
        int c=a-b;
        System.out.println("Difference="+c);
    }
}
 
I get the below error when I don't have the non-parametrized constructor parent()
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor at Child.<init>(InheritanceExample.java:14) at InheritanceExample.main(InheritanceExample.java:22)
 
 
The first thing the child class constructor must do is call the parent class constructor. If you do not do this explicitly (e.g. super(a,b)), a call to the default constructor is implied (super()).
For this to work, you must have this default constructor (without any parameters).
If you do not declare any constructors, you get the default constructor. If you declare at least one constructor, you do not get the default constructor automatically, but you can add it again.
The error message you are getting is complaining about the implied call to super() not working, because there is no such constructor in the parent class.
Two ways to fix it:

  1. add a default constructor
  2. in the first line of the child constructor, call a non-default parent constructor (super(a,b))
Also, please don't use all-lowercase class names.

The reason it is asking for parent() is because child extends parent, but you do not explicitly call super(a,b) in the child constructor. Since there is no explicit call to the parent constructor, javac tries to call the default constructor parent() and complains when it can't find it.

class parent
{
   public parent() {
      System.out.println("Parent Constructor");
   }

   public parent(int a,int b) {
      int c=a+b;
      System.out.println("Sum="+c);
   }

   public void display() {
      System.out.println("Return Statement");
   }
}

class child extends parent
{
   public child(int a,int b) {
      int c=a-b;
      System.out.println("Difference="+c);
   }
}

public class InheritanceExample
{
   public static void main(String args[]) {
      child c=new child(2,1);
      c.display();
   }
}
 
Output:
Parent Constructor Difference=1 Return Statement


class parent
{
   public parent(int a,int b) {
      int c=a+b;
      System.out.println("Sum="+c);
   }

   public void display() {
      System.out.println("Return Statement");
   }
}

class child extends parent
{
   public child(int a,int b) {
      super(a,b);
      int c=a-b;
      System.out.println("Difference="+c);
   }
}

public class InheritanceExample
{
   public static void main(String args[]) {
      child c=new child(2,1);
      c.display();
   }
}
Output:
Sum=3
Difference=1
Return Statement




I think there was a similar question at:
why should the derived class constructor always access base class constructor?
You can think of it this way: since "child" inherits from "parent", "child" must also be a valid instance of "parent" (polymorphism) before it can behave as a "parent" itself. As such, the very first thing "child" must do is construct itself as a valid "parent" - so a call to "parent"'s constructor via super() must be the first method call in the constructor. If no such call is present, an implicit call to the no-parameter constructor of "parent" results.

he error is there for the reason that if we do not call super explicitly then JVM puts super() in the constructor of the child class and this super() searches a constructor in parent class without parameter which is not in your class so it is wrong. Either put a non parametrised constructor in parent class or put the statement super(a,b) in the very first line of the child constructor


class Parent 
    {
        Parent(int a, int b) 
       {
            int c = a + b;
            System.out.println("Sum=" + c);
           }
        void display() 
    {
         System.out.println("Return Statement");
        }
    }
    class Child extends Parent 
    {
        Child(int a, int b) 
    {
        super(a,b);
        int c = a - b;
             System.out.println("Difference=" + c);
       }
    }
    class InheritanceExample 
    {
        public static void main(String args[]) 
    {
             Child c = new Child(2, 1);
        c.display();
       }
   }