Consider a shop where there are two types of customers Regular and Privileged customer
what is the difference between them:
Regular Customer:- These type of customer get discount of some percentage on the products
Privileged Customer:-These type of customers get gift on their membership card
How this could be implemented?
by Inheritance
Here Customer class is generalized once and Regular and Privileged Customers
- Inheritance is a mechanism inn which class A inherit all the properties of class B and said "class A inherit class B"
- All the methods and variables are available to class A
- A class which is inherited (here class B) is called Super class,Base class or Parent class
- A class which is inherits from another class (here class A) is called Derived class or Child class
- In Java, keyword used is "extends"
- Inheritance promotes reuse. if type of customer id to be added then we can use the Customer details form Customer class

- Multiple Inheritance

Multiple Level Inheritance is not allowed in java classes but they are implemented in Interfaces
Thinks of the scenario Retails store case study where customers are of two type Regular and Privileged customers what are the attributes of
Regular customer
Attribute : discount
Privileged customer
Attribute : membership card
Both the type of customer have different attributes now what are similar attributes
Attributes : customer name
customer id
customer address
Lets draw a class diagram and understand the concept

class Customer{
private int
customerId;
public void
setCustomerId(int customerId){
this.customerId=customerId;
}
public int
getCustomerId(){
return customerId;
}
}
class RegularCustomer extends Customer{
private float
discount;
public void
setDiscount(float discount){
this.discount=discount;
}
public float
getDiscount(){
return discount;
}
}
class PrivilegedCustomer extends Customer{
private String
memCardType;
public void
setMemCardType(String cardType){
this.memCardType=cardType;
}
public String
getMemCardType(){
return memCardType;
}
}
class Retail{
public static void main(String args[]){
RegularCustomer
regObj=new RegularCustomer();
PrivilegedCustomer
prvObj = new PrivilegedCustomer();
regObj.setCustomerId(1001);
regObj.setDiscount(20.00f);
prvObj.setCustomerId(2001);
prvObj.setMemCardType("Gold");
System.out.println("Regular Customer Details");
System.out.println("Customer Id:" +regObj.getCustomerId());
System.out.println("Discount:"+regObj.getDiscount());
System.out.println("Privileged Customer Details");
System.out.println("Customer Id:"+prvObj.getCustomerId());
System.out.println("Membership Card
Type:"+prvObj.getMemCardType());
}
}
Output:
Regular Customer Details
Customer Id: 1001
Discount: 20.0
Privileged Customer
Details
Customer Id: 2001
Membership Card Type: Gold
What is the Output?
class Base{
public Base(){
System.out.println("Base
class constructor");
}
}
class Der extends Base{
public Der(){
System.out.println("Derived class constructor");
}
}
class Demo{
public static void
main(String args[]){
Der Derobj=new
Der();
}
}


0 comments :
Post a Comment