Inheritence in Constructor

 package com.company;


class base{
base(){
System.out.println("I am a constructor ");
}
base(int a){
System.out.println("I am a over loaded constructor of base class with the value a given is " +a);
}

}

class Derived extends base{
Derived(){
// super(0);//super is a keyword where the only argument constructor will run from the base class
System.out.println("I am a derived class constructor ");
}
Derived(int x,int y){
super(x);//It takes the value of x from the argument constructor of derived class
System.out.println("I am a over loaded constructor of derived class with the value y given as " +y);
}


}
public class Inheritence {
public static void main(String[] args) {

// base b=new base();
Derived der =new Derived(10,12);//First it takes the constructor from the base then it to derived

}
}

Comments

Popular posts from this blog

Inheritence

Number_guessing_game

Dynamic_method_dipatch