0

Hey Guys,

I have the following code:

class A
{
	int a,b;
	A(int i,int j)
	{
		a=i;
		b=j;
	}

	void showab()
	{
		System.out.println(a+" and "+b);
	}
}

class B extends A
{
	int k=20;
	void showabk()
	{
		System.out.println("a="+a);
		System.out.println("b="+b);
		System.out.println("k="+k);
	}
}

class Test
{
	public static void main(String args[])
	{
		A obj1=new A(15,30);
		B obj2=new B();
		obj1.showab();
		obj2.showabk();
	}
}

When I compile the above code, I get the following error:

"Cannot Find Symbol Constructor A()"


What is the reason for this error and what is the solution to it.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by anuj_sharma
0

A subclass must call a constructor of the super class. If you, as the programmer, do not program a specific super call in the constructor of the subclass the compiler will automatically add a call to the default constructor of the super class. Unfortunately, when the super class defines a constructor, and does not define a default constructor, then the super class does not have a default constructor and you get exactly the error message you are seeing there. So, either define a constructor in the subclass and have it make a specific super call, or define a default constructor in the super class.

0

Thanks for the info.

Used the following code and it worked:

class A
{
	int a,b;
	
	A(int i,int j)
	{
		a=i;
		b=j;
	}

	void showab()
	{
		System.out.println(a+" and "+b);
	}
}

class B extends A
{
	B()
	{
		super(10,20);
	}
	
	int k=20;
	
	void showabk()
	{
		System.out.println("a="+a);
		System.out.println("b="+b);
		System.out.println("k="+k);
	}
}

class Test
{
	public static void main(String args[])
	{
		A obj1=new A(15,30);
		B obj2=new B();
		obj1.showab();
		obj2.showabk();
	}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.