DEV Community

flevia g
flevia g

Posted on

Single inheritance with constructor and Method Hiding

Single Inheritance:

package constructor;//Using single inheritance

public class Parentconstructor {

String name;
int empid;
public Parentconstructor(String name,int empid) {
    this.name=name;
    this.empid=empid;
    System.out.println("Employee Constructor Called");
    System.out.println("Name: " + name + ", ID: " + empid);

}

public static void main(String[] args) {



}
Enter fullscreen mode Exit fullscreen mode

}

package constructor;

public class Childconstuctor extends Parentconstructor{

String techStack;

public Childconstuctor(String name, int empId, String techStack) {
    super(name, empId);  // Call to parent constructor
    this.techStack = techStack;
    System.out.println("Developer Constructor Called");
    System.out.println("Technology: " + techStack);
}

public static void main(String[] args) {
    Childconstuctor cc = new Childconstuctor("raj",1143,"JAVA");


}
Enter fullscreen mode Exit fullscreen mode

}

Method Hiding:

package methodhiding;

public class Company {

public static void companyPolicy() {
    System.out.println("Company: General policy");
}


public static void main(String[] args) {
    // TODO Auto-generated method stub

}
Enter fullscreen mode Exit fullscreen mode

}

package methodhiding;

public class Branchoffice extends Company{

public static void companyPolicy() {
    System.out.println("Branch Office: Specific policy");
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    Company cp = new Branchoffice();
   companyPolicy();  // Calls Company version (reference type)

    Branchoffice bh = new Branchoffice();
   companyPolicy();  // Calls BranchOffice version
}

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)