3

Below is the example for Inheritance

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();
    }
}

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)

Can you please explain me what is the purpose of the constructor without parameters in base class.

5
  • 1
    it's just a default constructor that creates an instance of the parent class, in this case. usually you would set the object variables to some default value, or null. Commented May 31, 2011 at 1:35
  • Please include the text of the error, not a screenshot. Commented May 31, 2011 at 1:37
  • is that truly the source code? it compiles and runs fine for me via copy/paste. Commented May 31, 2011 at 1:44
  • @TofuBeer: It compiles if you include the constructor without parameters in the base class. If not, it does not. Commented May 31, 2011 at 1:48
  • Is Java convention to use uppercase in classes and opening brace in the same line. Commented May 31, 2011 at 2:30

6 Answers 6

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

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

You can see this with this code:

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

Also, this works fine with no default constructor:

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

Comments

0

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.

Comments

0

The 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();
       }
   }

Comments

0
public class Mobile{
private String manufacturer;
private String operating_system;
public String model;
private int cost;

Mobile(String man, String o,String m, int c){
this.manufacturer=man;
this.operating_system=o;
this.model=m;
this.cost=c;
}
public String getModel(){
return this.model;
}
}

public class Android extends Mobile{
Android(String man, String o,String m, int c){
super(man,o,m,c);
}
public String getModel(){
return "This is Android mobile" +model;
}

Comments

0
import java.io.*;
public class XXX
{
public static void main()throws IOException
{
System.out.println("Enter your name.");
String name = in.readLine();
System.out.println(name+" rules!! Thank You!!");
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.