File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is it create object of Base class? 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 "is it create object of Base class?" Watch "is it create object of Base class?" New topic
Author

is it create object of Base class?

Vilas Lawande
Ranch Hand

Joined: Nov 07, 2006
Posts: 127
hi Ranchers,

when we create object of Derived class,it will access method of Base class.
During this process,Is it create object of Base class? (because Derived class constructor call Base class class constructor means it will create object of Base class)
Deepak Giri
Ranch Hand

Joined: Feb 12, 2009
Posts: 52
interesting (if actually i got it)!
waiting for someone to reply


SCJP
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Are we talking here of Parent and child class? So in my interpretation three questions,:

1. When we create a child (derived)class, can it access a method of its Parent(base) class (Superclass)?
2. During the hierarchy class construction is the superclass created when we call an instance of the child class?
3. Is this represented in the constructor?



1. It depends on the access modifier of the method we are trying to access, is it visible to the child or not,.
this can also depend on if the child (derived) class is in the same package as its parent (the base class).
If the method we are trying to access is private, then unless the derived class is an inner-class it cannot see the
method (it is not visible). For more information, have a look for tutorials and info on access modifiers.

2.Yes, when an application is first loaded into memory, the JVM by the Java Magic checks all the members
and classes that require memory, and when a specific class instance is found, it also loads all classes,objects and paraphernalia,
all the way up the inheritance tree all the way up to our good old friend java.lang.Object.

3.Certainly it may be implicit and hidden( if we do not supply any explicit calls), but constructors will always be fed a
nice chunky call to super() to get them in the inheritance mood.

Hope this is near enough what you meant, if not, apologies.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Vishal Srivastav
Ranch Hand

Joined: Nov 29, 2008
Posts: 46

Nice question...

What i can think and answer this question is an object is created if and only if it is used by the new keyword.

Suppose the code...


When the above code finishes... Only one object is eligible for Garbage Collection i.e., objectOfB. Then how can an object of class A could have been created, or an object of class java.lang.Object could have been created?

Hope this might be helpful to you...

Anyone's comment and suggestions to my answer? I would like to know if I am wrong...


Vishal Srivastava, Software Engineer (Android), Paradigm Creatives
SCJP 5, SCWCD 5 - http://in.linkedin.com/in/srivastavavishal
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Vilas Lawande wrote:hi Ranchers,

when we create object of Derived class,it will access method of Base class.
During this process,Is it create object of Base class? (because Derived class constructor call Base class class constructor means it will create object of Base class)



Keep in mind that a derived class IS-A based class. That means there are portions of the derived object that is defined by the based class. When the constructor of the derived class calls the constructor of the based base, don't think of it as instantiating another object... Think of it as constructing the portion of the derived instance, that is defined by the based class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
1. Child can access member of the parent if they are in the same package and the member's access is default, protected or public
2. Child can access member lf parent if child is not in same package as parent, and member's access is protected or public
2. Child can never access member if member is private.
Vilas Lawande
Ranch Hand

Joined: Nov 07, 2006
Posts: 127
Thanks for posts.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
victor kamat wrote:
2. Child can never access member if member is private.


True, unless the class is an inner class, and then it may access the private members of its outer class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it create object of Base class?