Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


ajeesharjunan
constructors
1 Answer(s)      5 years and 10 months ago
Posted in : Java Beginners



Advertisement
View Answers

October 29, 2008 at 10:18 AM


Hi friend,


Constructors cannot be inherited, basically a constructor is not a method.Constructors are used to give a valid state for an object at creation. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.


class A{
A(){
/*do something*/
}
}
class B extends A{
B(){
/*do something*/
}
public static void main(String[] args){
A a = new A();
B b = new A();//is'nt this confusing?
}

----------------------------------------

Visit for more information.

http://www.roseindia.net/java/beginners/constructoroverloading.shtml

Thanks.


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com