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) {
}
}
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");
}
}
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
}
}
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
}
}
Top comments (0)