+ 1
Constructor can be override or not ??
6 Antworten
+ 4
No. Why do you want to do that? Keep in mind that the constructor of the super class is always invoked, when the extended class is constructed.
But constructors can be overloaded.
+ 2
// You may test it
public class A {
A(){
System.out.println ( "A()") ;
}
public void method(){
System.out.println ( "method() of A") ;
}
}
public class B extends A {
B(){
System.out.println ( "B()") ;
}
public void method(){
System.out.println ( "method() of B" ) ;
}
}
public class Program
{
public static void main(String[] args) {
A a = new A ();
System.out.println ("----------");
A b = new B ();
System.out.println ("----------");
a.method ();
System.out.println ("----------");
b.method ();
}
}
+ 1
Simply put no.
A constructor is a special method used to initialize a class object. Unlike the other methods of a class that you inherit from it can not be overridden. If you were able to do so, it would most likely lead to breaking the parent class, causing a loss of funtionality, and causing errors.
+ 1
No, a java constructor cannot be overwritten.
0
No
- 1
Yep constructor can be overloaded but can't be override in java don't know other languages