How to avoid a constructor of class A be extended to Class B?
Abstract Class A{
A(int)
{
}
}
Class B extends A{
}
now wen i compile class B. i get a error cannot find symbol: symbol is constructor A()
Wat do i have to do now?
Re: How to avoid a constructor of class A be extended to Class B?
When a subclass object is constructed, the superclass's constructor must be invoked - either explicitly or implicitly. In your case, it's invoked implicitly, and implicit constructor is constructor without arguments. But A does not have such a constructor. Hence an error.
Re: How to avoid a constructor of class A be extended to Class B?
By Default only non parametric construstor is called but since class A has parametric constructor you need to call it from class B
Re: How to avoid a constructor of class A be extended to Class B?
To add to what has already been said:
When you make a class which has a constructor with or without parameters, and you do not specifically include a constructor (or several constructors) it is the same thing as saying "only allow the listed constructors". So in your example when B attempts to extend A, the error says that B is attempting to use a constructor that does not exist (the default no parameter one implicitly being called)
Provide the no parameter constructor in class A or construct B with an explicit call to super(int)
Re: How to avoid a constructor of class A be extended to Class B?
Quote:
Originally Posted by
jps
To add to what has already been said:
When you make a class which has a constructor with or without parameters, and you do not specifically include a constructor (or several constructors) it is the same thing as saying "only allow the listed constructors". So in your example when B attempts to extend A, the error says that B is attempting to use a constructor that does not exist (the default no parameter one implicitly being called)
Provide the no parameter constructor in class A or construct B with an explicit call to super(int)
ok ..Guyz i took you were suggestion and modified the code as following:
************************************************** ***
public abstract class Zee{
Zee(int x)
{System.out.print("Zee 's constructor is callled///..........");
System.out.print("x value is " + x);
}
}
class MTV extends Zee{
private int a=10;
MTV(int x)
{
super(x);
}
}
import java.io.*;
public class Callo
{
public static void main(String [] args)
{
MTV w= new MTV(2);
}
}
now this Callo class wen compiled pops me error
java 1 class ,interface ,or enum expected
What s going wrong with my Callo class?
Re: How to avoid a constructor of class A be extended to Class B?
Please use code tags when posting code to the forum. Help can be found on the Announcements page.
Was Callo defined in it's own file?
Re: How to avoid a constructor of class A be extended to Class B?
Quote:
Originally Posted by
jps
Please use code tags when posting code to the forum. Help can be found on the
Announcements page.
Was Callo defined in it's own file?
Yes sir i hav put Callo class in Callo.java file n i compiled it. Am a beginner n am unable to figure out y it wont compile.