• Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects of abstract class  RSS feed

 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are objects created of abstract classes. I know you can't instantiate the class directly but if the extending class needs to constructed it cannot be complete without its parent and without the construction of its object.

If the constructor of a class runs should it always result in creation of an object ? One can place a constructor inside an abstract class.

For example, in the following program will an object be created of the abstract class SuperA ?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A SubA object is created (you only have one new statement, so you only create one object). But this SubA object is also a SuperA object.

Don't think of constructors as creating an object - I think that's what's causing your confusion. When a single object is created it's entirely possible that several constructors get run. It's better to think of constructors as initialising a new object. And even if a class is abstract, when it's instantiated via a subclass there might be initialisation code that needs to be run for it (e.g. instance variables declared in the superclass might need initialising).
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:A SubA object is created (you only have one new statement, so you only create one object). But this SubA object is also a SuperA object.


You mean there is only one object creation per 'new' usage ? Can you elaborate more on the latter statement where you say SubA also a SuperA object ?

Matthew Brown wrote:
Don't think of constructors as creating an object - I think that's what's causing your confusion. When a single object is created it's entirely possible that several constructors get run. It's better to think of constructors as initialising a new object. And even if a class is abstract, when it's instantiated via a subclass there might be initialisation code that needs to be run for it (e.g. instance variables declared in the superclass might need initialising).


What about java reflection. Aren't constructors used to create/construct objects using reflection ? Please correct me if i am wrong.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:You mean there is only one object creation per 'new' usage ? Can you elaborate more on the latter statement where you say SubA also a SuperA object ?


That's what inheritance means - there is an IS-A relationship. It's easier to see with more natural classes (rather than artificial ones like SubA). To pick an overused example:
Here, we say a Dog IS-AN Animal. An instance of the Dog class is also an instance of the Animal class: if you use the instanceof operator to check it will return true.

adithya narayan wrote:What about java reflection. Aren't constructors used to create/construct objects using reflection ? Please correct me if i am wrong.

Constructor objects are, but that's a bit different. Creating the object will still cause the constructor to be executed for the class you're instantiating and every super class.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean there is only one object creation per 'new' usage ? Can you elaborate more on the latter statement where you say SubA also a SuperA object ?


Look at this:


Cat is an animal, But we have only one CAT!!

All right?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
You mean there is only one object creation per 'new' usage ? Can you elaborate more on the latter statement where you say SubA also a SuperA object ?


Look at this:


Cat is an animal, But we have only one CAT!!

All right?



you are confused between the terminology. in the above example of yours, yes there is only one Cat. And this one Cat IS-A Animal. Cat IS-A animal does not mean there is a Cat and there is a Animal . Cat IS-A Animal means Cat also inherits attribute of Animal. think in terms of logical terms. all cats are animals but not vice versa. this is inheritance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:you are confused between the terminology. in the above example of yours, yes there is only one Cat. And this one Cat IS-A Animal. Cat IS-A animal does not mean there is a Cat and there is a Animal . Cat IS-A Animal means Cat also inherits attribute of Animal. think in terms of logical terms. all cats are animals but not vice versa. this is inheritance.

I think that's exactly what Abalfazl meant.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. i overlooked that statement. Should have read it completely.

Matthew Brown wrote:
Constructor objects are, but that's a bit different. Creating the object will still cause the constructor to be executed for the class you're instantiating and every super class.


I was under the impression that whenever constructor runs, an object of the residing class gets created ! Thanks for clarifying this.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!