• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance - how many objects created???  RSS feed

 
Imran Mehrenali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



1) In the above code, when i create an object of Class B, how many objects will get created? Only one object of class B that has 2 members (a and b) or two objects - one of class A that has one member (a), and another of class B that also has only member (b) but can directly acccess the member (a) beloning to object of class A.

2) I was about to ask how many constructors will be called, but have tried this out myself in a java program and have found that 2 constructors (A() and B()) are called while creating an object of class B.
Does this mean that 2 objects are created.

Many Thanks.
[ October 29, 2007: Message edited by: Imran M ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to assume you meant to have class B extend class A in the above; otherwise the question doesn't make sense.

The answer is just one object. An instance of B includes an A "part". Imagine a piece of paper 1 inch square, call it A. Then think of a B object as a piece of paper 2 inches square, with a 1-inch square drawn onto it labeled "A". The A constructor is called to initialize the A part of the single B object.
 
Imran Mehrenali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edited the code. Original Post now reflects the right code. Apologies for the incorrect code.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Imran M", please check your private messages. You can see them by clicking My Private Messages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!