Important Concepts of Object-Oriented system (Part III)

//Employee.javapackage com.thirstybrain.app;public abstract class Employee {private int id;
private String name;
public Employee(int id,String name){
this.id=id;
this.name=name;
System.out.
println("Invoked form abstract class Employee contructor ");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmployeeDetail(){
System.out.
println(“Invoked from Employee class getEmployeeDetail method”)
return
"Id : "+id+" Name : "+name;
}
public abstract double getSalaryDetail();
}
//FulltimeEmployee.javapackage com.thirstybrain.app;import com.thirstybrain.app.Employee;public class FulltimeEmployee extends Employee{
private double salary;
public FulltimeEmployee(int id, String name,double salary)
{
super(id, name);
this.salary=salary;
System.out.
println("Invoked form class Employee contructor ");
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}

public String getEmployeeDetail(){
System.out.println("Invoked from FulltimeEmployee class ,getEmployeeDetail() method");
return "";
}
@Override
public double getSalaryDetail() {
return salary;
}
}//Program.Javapackage com.thirstybrain.app;import com.thirstybrain.app.FulltimeEmployee;public class Program {public static void main (String [] args) {//1. Constructor calling sequence

Employee employeeObjC=new FulltimeEmployee(1, "shivam",10000.00);
//Output
//Invoked form abstract class Employee contructor
//Invoked form class Employee contructor
//Reason: Since, we are doing constructor chaining thats why
//base class constructor is first get called
//and then child class constructor gets called.
FulltimeEmployee fulltimeEmployeeObjC=
new FulltimeEmployee(2, "Raj", 100000.00);
//Output
//Invoked form abstract class Employee contructor
//Invoked form class Employee contructor
//Reason: Since, we are doing constructor chaining thats why
//base class constructor is first get called
//and then child class constructor gets called.
Employee employeeObj = new FulltimeEmployee(1, "shivam",10000.00);
employeeObj.getEmployeeDetail();
//Output: -
//Invoked form abstract class Employee contructor
//Invoked form class Employee contructor
//Invoked from FulltimeEmployee class, getEmployeeDetail() method
//Reason: Since we are using object of FulltimeEmployee classs //i.e.child class.That's why getEmployeeDetail of
//FulltimeEmployee class child class get called.
FulltimeEmployee fulltimeEmployeeObj =
new FulltimeEmployee(1, "shivam",10000.00);
fulltimeEmployeeObj.getEmployeeDetail();
//Output: -
// Invoked form abstract class Employee contructor
//Invoked form class Employee contructor
//Invoked from FulltimeEmployee class, getEmployeeDetail() method
// Reason: Since we are using object of FulltimeEmployee classs //i.e.child class
//That's why getEmployeeDetail of FulltimeEmployee class
// i.e child class get called.
}}
//Employee.java
package com.thirstybrain.app.single;
public class Employee {
public int id;
public String name;
public void getEmployeeDetail(){
System.out.println("Invoked getDetail from Employee Class ");
}
}
//FulltimeEmployee.java
package com.thirstybrain.app.single;
public class FulltimeEmployee extends Employee {
public void getFulltimeEmployeeDetail(){
System.out.
println("Invoked getFulltimeEmployeeDetail from FulltimeEmployee Class ");
}}//Program.java
package com.thirstybrain.app.single;
public class Program {
public static void main(String[] args) {

FulltimeEmployee fulltimeEmployeeObj=new FulltimeEmployee();
fulltimeEmployeeObj.getEmployeeDetail();
//Output : Invoked getDetail from Employee Class
fulltimeEmployeeObj.getFulltimeEmployeeDetail();
//Output :
//Invoked getFulltimeEmployeeDetail from FulltimeEmployee Class
}
}
//Employee.java
package com.thirstybrain.app.multilevel;
public class Employee {
public int id;
public String name;
public void getEmployeeDetail(){
System.out.println("Invoked getDetail from Employee Class ");
}
}
//FulltimeEmployee.java
package com.thirstybrain.app.multilevel;
import com.thirstybrain.app.multilevel.Employee;public class FulltimeEmployee extends Employee {
public void getFulltimeEmployeeDetail(){
System.out.println("Invoked getFulltimeEmployeeDetail from FulltimeEmployee Class ");
}
}
//Manager.java
package com.thirstybrain.app.multilevel;
public class Manager extends FulltimeEmployee {
public void getManagerDetail(){
System.out.println("Invoked getManagerDetail from Manager Class ");
}
}
//Program.java
package com.thirstybrain.app.multilevel;
public class Program {
public static void main(String[] args ){
Manager managerObj=new Manager();
managerObj.getEmployeeDetail();
//Output : Invoked getDetail from Employee Class
managerObj.getFulltimeEmployeeDetail();
//Output :
//Invoked getFulltimeEmployeeDetail from FulltimeEmployee Class
managerObj.getManagerDetail();
//Output : Invoked getManagerDetail from Manager Class
}}
//Employee.javapackage com.thirstybrain.app.hierarchical;public class Employee {public void getEmployeeDetail(){
System.out.println("Invoked getEmployeeDetail from Employee class");
}
}//FulltimeEmployee.java
package com.thirstybrain.app.hierarchical;
public class FulltimeEmployee extends Employee {public void getFulltimeEmployeeDetail(){
System.out.println("Invoked getFulltimeEmployeeDetail from FulltimeEmployee class");
}}//ContractEmployee.java
package com.thirstybrain.app.hierarchical;
public class ContractEmployee extends Employee {
public void getContractEmployeeDetail(){
System.out.
println("Invoked getContractEmployeeDetail from ContractEmployee class");
}
}//Program.Java
package com.thirstybrain.app.hierarchical;
public class Program {
public static void main( String [] args){
ContractEmployee contractEmployee =new ContractEmployee();
contractEmployee.getContractEmployeeDetail();

//Output:
//Invoked getContractEmployeeDetail from ContractEmployee class
contractEmployee.getEmployeeDetail();
//Output: Invoked getEmployeeDetail from Employee class
}}
//Manager.java
package com.thirstybrain.app.multiple;
public class Manager {
public void getDeveloperDetail(){
System.out.println("Invoked getManagerDetail from Manager Class ");
}
}//Developer.java
package com.thirstybrain.app.multiple;
public class Developer {
public void getDeveloperDetail(){
System.out.println("Invoked getDeveloperDetail from Developer Class ");
}
}
//Employee.java
package com.thirstybrain.app.multiple;
public class Employee extends Manager,Developer{//it will give error at compile time because java doesn't supports
//Multiple inheritance
public void getDeveloperDetail(){
System.out.
println("Invoked getDeveloperDetail from Developer Class ");
}
}
Employee employee =new Employee();
employee.getDeveloperDetail();
//Reports.java
package com.thirstybrain.app.multiple;
public interface Reports {
public static final String companyName="thirstyBrain";
void getReport();
}
//PowerBIReport.java
package com.thirstybrain.app.multiple;
public class PowerBIReport implements Reports { @Override
public void getReport() {
System.out.
println("Invoked getReport from PowerBIReport Class ");
}
}
//D3Report.java
package com.thirstybrain.app.multiple;
public class D3Report{ public void getReport(){
System.out.println("Invoked getReport from D3Report Class ");
}
}
//Program.java
package com.thirstybrain.app.multiple;
public class Program extends D3Report implements Reports {
public static void main(String[] args) {
////case 1
Reports report = new PowerBIReport();
report.getReport();
////Output : Invoked getReport from PowerBIReport Class
System.out.
println("Company Name is : "+ Reports.companyName);
////Output : Company Name is : thirstyBrain

//Case 2
D3Report d3ReportObj=new D3Report();
d3ReportObj.getReport();
////Output : Invoked getReport from D3Report Class
}
}
public static final String companyName="thirstyBrain";
public class Person{
public void getPersonDetail(){
System.out.println("getPersonDetail() method of Person");
}
}
public class Developer extends Person{
public void show(){
System.out.println("show() method of Developer");
}

public void getDeveloperDetail(){
System.out
.println("getDeveloperDetail() method of Developer");
}
}
public class Manager extends Person{ public void show(){
System.out.println("show() method of Manager");
}
public void getManagerDetail(){
System.out.println("getManagerDetail() method of Manager");
}
}
public class Employee extends Developer,Manager{ public void getEmployeeDetail(){
System.out.println("getEmployeeDetail() method of Employee");
}

public static void main(String args[]){
Employee employeeObj = new Employee();
employeeObj.getEmployeeDetail();
employeeObj.show();
//Confusion happens here which show method to call
}
}
//Output : This code will give us compile-time error because of the ambiguity problem

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store