• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it create object of Base class?

 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interesting (if actually i got it)!
waiting for someone to reply
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Srivastav
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posts.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic