This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super & Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super & Abstract class" Watch "super & Abstract class" New topic
Author

super & Abstract class

Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Hi.
I am having problems with the following code...
---------------------------------------------------
public class Base {
Base() {
System.out.println("In Base Class no args Constructor");
}
Base(int i) {
System.out.println("In Base Class one arg Constructor");
}
public abstract class AbsDerived extends Base {
void myFunc();
}
public class Derived extends AbsDerived {
public Derived() {
super(); // Line 1
}

public Derived(int i) {
super(1) // Line 2
}
void myFunc() {
System.out.println("I do nothing");
}
}
---------------------------------------------
The problem is ... Line 2 gives a compiler error. It says that there is no matching constructor in class AbsDerived.
However, if we comment out line 2, the code compiles fine & when class Derived is instantiated, it correctly calls the Base Class's no argument constructor.
Why do we have problems with compiling super with arguments... ??
Please help...
thanks
- Himanshu


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Constructors are not inherited.
If you want to make both of your constructors in class Base available to Derived you have to declare the same constructors in AbsDerived which call super() with the appropriate arguments.

this way it works... You cannot invoke directly the constructor of Base from within Derived... you have to go through AbsDerived and the only way to do it is to declare the same-signature constructors in AbsDerived
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Thanks for the quick reply.. Valentin, that helps.
Have one more question, if I have constructor in an abstract class, isn't it like instantiating the abstract class ??
Maybe not, because we will always do a new on the Derived class, never on the AbsDerived class, correct ?
Please comment.
thanks.
Himanshu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
that's right. The fact of having a constructor in your abstract class doesn't mean you can instantiate the abstract class directly. Such a constructor is useful for setting up proprietary things of your abstract class, but the actual instantiation of the abstract class is done by instantiating a concrete class that extends your abstract class.
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super & Abstract class
 
Similar Threads
Abstract super class.
doubt regarding constructors
A simple Q but still have doubt
Doubt in Mock Question
if super class is abstract, subclass must be abstract?