aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Hi,
Please explain the following behaviour:
When I declare the Base class constructor as private then I am not able to instantiate the Base class.That is fair enough.It is also fine that when I instantiate the Derived class without having its own constructor to throw an error bcoz then it will invoke the Base class constructor and it is private.But why am I unable to instantiate the Derived class extending the Base class when it(the Derived class)has its own public constructor.
The code below will further clarify the question:
class Base
{
private Base()
{
System.out.println("In Base class [constructor]");
}
}
public class Derived extends Base
{
public Derived()
{
System.out.println("In Derived class [constructor]");
}
public static void main(String args[])
{
Derived d = new Derived();
}
}
Error: bcoz Base has private access.
[Also the super class constructor is called only when you explicitly call it by using the
method super().So by default constructors are not inherited whereas methods are.]
Please explain as I have not understood the concept thoroughly.
Thanks in advance.
Regards,
Kavita.
[This message has been edited by Kavita Ghia (edited April 13, 2001).]
[This message has been edited by Kavita Ghia (edited April 13, 2001).]
Amitabh Yadav
Greenhorn

Joined: Apr 07, 2001
Posts: 13
Hi Kavita
In ur code is there any class extending to base class so that inheritance can be established???.
private methods can only be acessed by the instance of that class in which the method is written .If we subclass them we can;t acess those methods .
we can have private constructor but to make a object using that constructor u need to have a public method out there in parent class which in turns call the private contructor of the class thus makign an object.

Tell me if i am wrong .

[This message has been edited by Amitabh Yadav (edited April 13, 2001).]
Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Hi,
I have edited the the above code to extend Base class.
To be very specific my question is How are constructors and methods inherited withe various access modifiers?
Kavita.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
There is no such thing as inheritance of constructors. Constructors belong to the class and are called when you created new objects. They are also called when subclass objects are created.
Just like private methods, private constructors are not even visible outside the class.
Does that answer your question??


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974

The output of this is:
Parent Constructor
Child Constructor
Notice that the default parent constructor executes before the child constructor. Now you can see that your statement:
[ Also the super class constructor is called only when you explicitly call it by using the
method super().So by default constructors are not inherited whereas methods are. ]
is not correct. The super class constructor is called.
[This message has been edited by Thomas Paul (edited April 13, 2001).]
[This message has been edited by Thomas Paul (edited April 13, 2001).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
sandeep bagati
Ranch Hand

Joined: Feb 22, 2001
Posts: 62
Originally posted by Kavita Ghia:
Hi,
I have edited the the above code to extend Base class.
To be very specific my question is How are constructors and methods inherited withe various access modifiers?
Kavita.

As said above there is nothing like inheritance of a Constructor for it doesnt mean anything .
Now regarding to yr question why then i get an error when i declare base class Constructor PRIVATE is that when u create a new Object of a class somewhere down in hierarchy it has somehow to reach to object class's default Constructor really while u invoke the new keyword for creating the creation of object.Since java doesnt support multiple inheritance so u can go just one level up yr hierarchy calling a constructor of its immediate superclass.Now the Constructors behave like functions i:e just like function the superclass should have a constructor with same parameters as is called by the subclass.
E;g if u have got a simple class say A{} then their is an implict call to its immediate class Object or u can write
A{ super();}.
NOW AS FOR AS ACCESS THEY FOLLOW THE SAME RULES AS FUNCTIONS.
AND ONE MORE THING THE EXECUTION STARTS IN THE REVERSE ORDER THATIS FROM TOPMOST CLASS DOWN THE HIERARCHY TO THE CLASS OBJECT WHICH IS BEING CREATED.
IT ACTS LIKE A LADDER wHERE THE AIM IS TO REACH TOP GET SOMETHING AND COME BACK AND THE INTERMEDIATE CONSTRUCTORS ACT AS STEPS.

SANDEEP


[This message has been edited by sandeep bagati (edited April 13, 2001).]
Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Hi Sandeep,
I did follow your explanation but got one point to confirm.
Does it means that if your Derived class has a default constructor still it will go up the heirarchy(ONLY to the IMMEDIATE super class)and call the super class constructor implicitly and then its own constructor the way Paul's(OUR BARTENDER) example shows the output.And accordingly the derived class constructors will behave.
Therefore the following implies:
IF SUPER CLASS CONSTRUCTORS ARE:
1.PRIVATE => cannot instantiate subclass
2.PROTECTED=> cannot instantiate subclass
3.DEFAULT=>can instantiate subclass
4.PUBLIC=>can instantiate subclass
(Ofcourse the same will apply to the super class as well.)
Thanks,
Kavita.
sandeep bagati
Ranch Hand

Joined: Feb 22, 2001
Posts: 62
Originally posted by Kavita Ghia:
Hi Sandeep,
I did follow your explanation but got one point to confirm.
Does it means that if your Derived class has a default constructor still it will go up the heirarchy(ONLY to the IMMEDIATE super class)and call the super class constructor implicitly and then its own constructor the way Paul's(OUR BARTENDER) example shows the output.And accordingly the derived class constructors will behave.
Therefore the following implies:
IF SUPER CLASS CONSTRUCTORS ARE:
1.PRIVATE => cannot instantiate subclass
2.PROTECTED=> cannot instantiate subclass
3.DEFAULT=>can instantiate subclass
4.PUBLIC=>can instantiate subclass
(Ofcourse the same will apply to the super class as well.)
Thanks,
Kavita.

yah it has to follow the ladder.
Now if we have got just a private Constructor in a class we can never instantiate it.A Good example is the MATH class in api.Check out if u can instantiate it.And then read the Constructor(s) of math class in api.
Second thing if u have got a only a Constructor with a default access specifier u can never subclass it outside the package.
Yah same rules as functions apply here.
But one thing modifiers like static,native etc dont have any relevence to constructors and hence u can not use them
And one more thing if u have got something like this native void A(){} Where A is the class name A() is a function here.I think the return Type differentiates the two cases.
sandeep
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance