Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
class A
{
  int i,j;
  A(int i,int j)
  {
    this.i=i;
    this.j=j;
  }
  void show()
  {
    System.out.println("i="+i+" "+"j="+j);
  }
}

class B extends A
{
  int k;
  B(int k)
  {
    this.k=k;
  }
  void showsub()
  {
    System.out.println("i+j+k= "+(i+j+k));
  }
}

class inherit
{
  public static void main(String args[])
  {
    A obj1=new A(3,5);
    B obj2=new B(7);
    obj1.show();
    obj2.showsub();
  }
}


What I have tried:

I have tried this code but giving compile time error.
Posted
Updated 16-May-17 1:59am
v2

1 solution

The B class implicitely calls super() however no argument-less constructor is defined in the A class.
You might either:
  • call, for instance, super(0,0); inside B class constructor
or
  • define a argument-less constructor in the A class

See Default constructor - Wikipedia[^].
Using the Keyword super (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)[^] .
 
Share this answer
 
Comments
Richard MacCutchan 16-May-17 8:32am    
Another 5 for a good answer, and for pointing out my error in the previous question.
CPallini 16-May-17 13:17pm    
Thank you Richard, you are too good a man!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900