Δημοσιεύτηκε: 18 Ιαν 2012, 19:13
από Star_Light
Κώδικας: Επιλογή όλων
class examples{
int value1;
int value2;
examples(){
value1 = 1;
value2 = 2;
System.out.println("Inside 1st Parent Constructor");
}
examples(int a){
value1 = a;
System.out.println("Inside 2nd Parent Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
DemoChild d1 = new DemoChild();
d1.display();
}
}
class DemoChild extends examples{
int value3;
int value4;
DemoChild(){
//super(5);
value3 = 3;
value4 = 4;
System.out.println("Inside the Constructor of Child");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
System.out.println("Value1 === "+value3);
System.out.println("Value2 === "+value4);
}
}


Ποια η ροή του κώδικα εδω? :S εχω χασει λιγο τον μπούσουλα...

Owing to constructor chaining , when object of child class DemoChild is created , constructor Demo() of the parent class is invoked first and later constructor DemoChild() of the child is created.

αλλα δεν καταλαβαινω γιατι γινεται αυτο.

@giannosfor το ιδιο λεμε απλα εγω πριν το ειπα πιο μπερδεμενα -.-