<< Previous Chapter | Next Chapter >> |
Definitions:
Inheritance is the process in which a class can extend or inherit or derive another class and can reuse their property (data member & methods).
A class that is derived or inherited from another class is known as subclass or, derived class or, extended class, or child class. The class from which the subclass is derived is known as a super class or, base class or, a parent class.
As we know that Object is a top class in java class hierarchy, so obviously the Object class does not have any parent class, but except Object class all classes in java must have a one and only one super class (this is called single inheritance). If any class does not explicitly inherit any other class then it is implicitly inherited by Object class.
As we know that Object is a top class in java class hierarchy, so obviously the Object class does not have any parent class, but except Object class all classes in java must have a one and only one super class (this is called single inheritance). If any class does not explicitly inherit any other class then it is implicitly inherited by Object class.
Classes can be inherited from a class which further inherited from other class and so on.. and at the top of this hierarchy the root class will always be the Object class.
Example: let’s have a scenario where a manufacturing company is having 2 types of supplier, one who supplies parts for their gas engine and another who supplies general items like computer, cubical, water, disposables etc. So, all suppliers need some common fields like name, address etc. and some specific fields related to their supplied product. We are going to put all common fields in parent class and specific fields to their child classes.
Supplier.java
public class Supplier {
// 3 fields of Supplier class - declared as public and protected both for demo
public String supplierName;
public String supplierAddress;
protected String supplierContact;
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public String getSupplierAddress() {
return supplierAddress;
}
public void setSupplierAddress(String supplierAddress) {
this.supplierAddress = supplierAddress;
}
public String getSupplierContact() {
return supplierContact;
}
public void setSupplierContact(String supplierContact) {
this.supplierContact = supplierContact;
}
}
XYZLimited.java
//This is a gas engine part supplier
public class XYZLimited extends Supplier
{
//2 local fields of XYZLimited
public String partName;
public String partGasEngineType;
public String getPartName() {
return partName;
}
public void setPartName(String partName) {
this.partName = partName;
}
public String getPartGasEngineType() {
return partGasEngineType;
}
public void setPartGasEngineType(String partGasEngineType) {
this.partGasEngineType = partGasEngineType;
}
}
ABCLimited.java
//This is a general supplier
public class ABCLimited extends Supplier {
//2 local fields of ABCLimited
public int cubicalSize;
public String computerMonitorSize;
public int getCubicalSize() {
return cubicalSize;
}
public void setCubicalSize(int cubicalSize) {
this.cubicalSize = cubicalSize;
}
public String getComputerMonitorSize() {
return computerMonitorSize;
}
public void setComputerMonitorSize(String computerMonitorSize) {
this.computerMonitorSize = computerMonitorSize;
}
}
Implementation.java
public class Implementation {
public static void main(String[] args) {
XYZLimited partSupplier = new XYZLimited();
partSupplier.setSupplierName("Xyz Limited");
partSupplier.setSupplierAddress("XYZ Street, New York, USA");
partSupplier.setSupplierContact("+1 4444444444");
partSupplier.setPartGasEngineType("Electric");
partSupplier.setPartName("Valve Spring");
ABCLimited generalSupplier = new ABCLimited();
generalSupplier.setSupplierName("Abc Limited");
generalSupplier.setSupplierAddress("ABC Street, wisconsin, USA");
generalSupplier.setSupplierContact("+1 8888888888");
generalSupplier.setComputerMonitorSize("18 Inch");
generalSupplier.setCubicalSize(5);
System.out.println("Details of part material supplier: ");
System.out.println("Supplier Name: " + partSupplier.getSupplierName());
System.out.println("Supplier Address: " + partSupplier.getSupplierAddress());
System.out.println("Supplier Contact: " + partSupplier.getSupplierContact());
System.out.println("Gas Engine Type: " + partSupplier.getPartGasEngineType());
System.out.println("Part Name: " + partSupplier.getPartName());
System.out.println("\nDetails of part general supplier: ");
System.out.println("Supplier Name: " + generalSupplier.getSupplierName());
System.out.println("Supplier Address: " + generalSupplier.getSupplierAddress());
System.out.println("Supplier Contact: " + generalSupplier.getSupplierContact());
System.out.println("Gas Engine Type: " + generalSupplier.getComputerMonitorSize());
System.out.println("Cubical Size: " + generalSupplier.getCubicalSize());
}
}
Output:
No comments:
Post a Comment