Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritence

 
SAIMA RAFI
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can be done. Just fire up the compiler and see if it complains!

Bosun
 
SAIMA RAFI
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plz explain me how (in detail)
coz i tried this & it gives error
thanks
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic