Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
awad saleh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a constructor of a super class be inherited.? Wat does this line means? when we inherit a super class, is the super class constructed not inherited? Explain me with code snippet.
 
saurav sarkar
Ranch Hand
Posts: 180
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a sub class if you dont provide a constructor.......then it will
automatically call the no argumented constructor of the super class.

If the constructor is provided in the sub class, then through the super keyword the constructors of the super class can be inherited for both
argumented and non-agrumented.
whether implicit or explicit call of construtors 'constructor chaining
happens to the object class's constrtuctors.

Experts please correct me if i am wrong.
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inerit a constructor is not s proper definition. Your class inerit from its parent anyway you can say that the constructors of your your class can instantiate one of the constructors of its parent; which one is your choose.

If you have something like


when you intantiate


when you intantiate or


it automatically call the default constructor of the animal class Animal.

But if you instantiate

it will call the constructor of Animal which take a String as parameter.

This process can be replicated if you another class extends the Mammal class; take the example below



the constructor Dog(String name) will instantiate (in order) the following contructors
1) Animal()
2) Mammal(int numLegs)
3) Dog(String name)

on the other side the Dog() will instantiate
1) Animal(String name)
2) Mammal(String name)
3) Dog()

ciao
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic