The Artima Developer Community
Sponsored Link

Java Answers Forum
Question in Inheritance

3 replies on 1 page. Most recent reply: Oct 4, 2002 3:37 AM by Ana

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
krupa

Posts: 2
Nickname: krupaksk
Registered: Aug, 2002

Question in Inheritance Posted: Oct 3, 2002 6:46 AM
Reply to this message Reply
Advertisement
Hi All,
In any OOP Language constructors are used to create the object and allocating memory to the object right. As we all know when instantiating the subclass the super classe constructor is called. Why then the Constructor calling dont create any super class instance. I had tried printing the class name in the constructors of the super class and sub-class, both the names are sub class name.

For ex. We have class A,B. B extends A. In both the constructors this.getClass.getName() prints B.

Can someone clarify how the object creation is suppressed. If there is no object being created why to call super class constructor at all.
Regards,
krupa.


Ana

Posts: 12
Nickname: anika
Registered: Aug, 2002

Re: Question in Inheritance Posted: Oct 3, 2002 8:52 AM
Reply to this message Reply
Hi Krupa,
an instance of the superclass is indeed created.
Given B extending A with respective constructors:

A(){
System.out.println("This is A");
}

and

B(){
System.out.println("This is B");
}

the output when creating a new B will be:
This is A
This is B

which in fact shows that the A constructor was invoked before the B constructor.
The reason why it is not seen when using b.getClass().getName() is that the instance of A is wrapped to the object of the inherited class.

krupa

Posts: 2
Nickname: krupaksk
Registered: Aug, 2002

Re: Question in Inheritance Posted: Oct 3, 2002 9:44 AM
Reply to this message Reply
Hi Ana ,
Thanks for the reply. Actually i was also thinking similar. But nowhere i read about Inheritance mentions that subclass behaves wrapping the super class and also there are other few Grey areas with this concept which i explain below. ( If you got any article/book explaining the above concept, please send me the references.. )

1.Abstract classes: As we know we cannot create any instance of abstract classes. If we extend from a abstract class is the Instance of abstract class wrapped in the calling class, because no way we can create an instance of abstract class right.

2.Super key word: If the instance is wrapped inside the base class why it is not visible to the sub class. Whenever we want to access the super class methods we are using the super keyword. Why cant we use the wrapped object instead.... infact where is it located?

3. Object class: We all know Object is super class of all the classes. How many number of object class instances available in the environment for a big project?? staggering!!!

Regards,
krupa.

Ana

Posts: 12
Nickname: anika
Registered: Aug, 2002

Re: Question in Inheritance Posted: Oct 4, 2002 3:37 AM
Reply to this message Reply
Hi Krupa,
maybe you already know 'Thinking in Java' by Bruce Eckel. It kann be found at www.BruceEckel.com. It might help. If you find any other good references, could you write them here in the forum? Thanks

Ana

Flat View: This topic has 3 replies on 1 page
Topic: can we create a local variable for a thread?? Previous Topic   Next Topic Topic: stale connection exception with db2

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use