is it possible to reduce number of parameter passed to extended class than number of parameter in base class e.g if class tree has 3 parameters in constructor & its extended class Pine has 2 parameters 1 is from base class(through super) and 1 new parameter. class Tree{ String name; int no; int height; Tree(String a,int b,int c) { name=a; no=b; height=c; } void print(){ System.out.println(a+b+c); }} class Oak extends Tree { String type; Oak(String a, Stringd){ super(a); type=d; } void print() System.out.println(a+d); }
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
posted
0
Yes, it can be done. Just fire up the compiler and see if it complains!
Bosun
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
SAIMA RAFI
Greenhorn
Joined: Jul 06, 2001
Posts: 6
posted
0
plz explain me how (in detail) coz i tried this & it gives error thanks
Roy Tock
Ranch Hand
Joined: Jul 16, 2001
Posts: 83
posted
0
Saima, the problem here is that the Oak constructor is calling Tree(String), but no such Tree constructor exists. Rather than "super(a);", try "super(a, 1, 1);". Also, be sure to put a space between "String" and "d" in your Oak constructor's declaration, put a '{' at the beginning of Oak.print(), and close the class with '}'. (You'll find that keeping your curly braces on their own lines, and lining corresponding ones up in the same column, will help you to keep the brackets proper.) Further, remember that variables a, b, and c are local to the Tree constructor; you cannot reference them in Tree.print(), but you can reference name, no, and height. The same goes for class Oak; you cannot reference a and d in method Oak.print, but you can reference name and type. Making these mods, and adding a class with a main method, I get this:
The program's compile and output: F:\temp>javac Forester.java F:\temp>java Forester StrongbranchRed F:\temp>