• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inhertance

 
shruti patel
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we inherit the constructor in a Class?please give one
example.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shruti patel:
Can we inherit the constructor in a Class?
No
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you test whether constructors are inherited? Tell us what code you'd need to do so. Once you have the code, you can compile and run it, to see what happens.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default constructors are not inherited. You can call a superclass constructor in an inheritance chain by using the "super." keyword.
 
cheng jian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shruti patel:
Can we inherit the constructor in a Class?please give one
example.

For constructor we can do one thing.
Overloading!!
A example from SCJP

The inhertance is used for a class.
(Sorry i'm poor about english!)
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructors are not inherited

here's the code to support this



this is ConstructorTry class in A package

There is another class ConstructorTry in package B



the line marked will give an error that there is no constructor in Constructor try that takes a String. If the constructor of A.ConstructorTry was inherited then the line would have compiled as A.ConstructorTry has a constructor which takes a String and B.ConstructorTry is a sub-class of A.ConstructorTry.

Hope this is OK.......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic