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.

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.

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();
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.