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.

Dani AI

Generated

The compile failure is the classic implicit-super problem. As explained, when a subclass has no explicit constructor the compiler creates a default no-arg constructor for it and that constructor implicitly calls super(). If the superclass (here A) only declares a parameterized constructor and has no no-arg constructor, there is no A() for the implicit call to invoke, so compilation fails. fixed this by adding an explicit constructor to B that calls super(...), which is the usual remedy.

Practical fixes and guidance:

  • Give A a no-arg constructor (possibly delegating to the parameterized one) if a reasonable default state exists. Many frameworks also expect no-arg constructors, so this can help with deserialization/DI.
  • Add an explicit constructor to B that calls super(...) with appropriate values. Remember an explicit super(...) call must be the first statement in the constructor.
  • Or expose a constructor in B that accepts arguments and forwards them to A, so callers supply correct values and A’s invariants are preserved.

Two small, often-missed points: field visibility and initialization order. a and b in your code are package-private, so a subclass in a different package would need protected or accessors. Also, the superclass constructor completes before subclass instance initializers run, so subclass fields (like k) are not initialized until after the super constructor finishes. For details on Java constructors and super, see the Oracle Java tutorials: Constructors and super.

Recommended Answers

All 2 Replies

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.