Object-oriented programming System(OOPs) is a programming paradigm based on the concept of “objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs
The four building blocks of object oriented java are:
- Inheritance
- polymorphism
- Abstraction
- Encapsulation
Inheritance:
- The property of an object to acquire all the properties and behavior of its parent object is called inheritance
The keyword 'extends' is used to inherit the properties of a class.
Advantage: Reuse the fields and methods of the existing class
Types of inheritance:
- Single inheritance: Single level inheritance enables a child class to inherit properties and behavior from a single parent class
- Multilevel inheritance: Multi level inheritance enables a child class to inherit properties and behavior from a parent class which is also derived from another class
- Hierarchical inheritance: Hierarchical level inheritance enables more than one derived class to inherit properties and behavior from a parent class
- Multiple inheritance: One child class inherits properties and behaviors of two parent class. java does not support multiple inheritance.
Key points:
- It reduces the length of the code and reduces redundancy as we can reuse the code.
- In inheritance, it is possible to create the object of both parent and child class. If you are creating object of parent class, then it is possible to access only parent class properties. If you are creating object of child class, then it is possible to access both parent class properties as well as child class properties. In project level, it is recommended to create the object of child class.
- There are two relationship in inheritance, IS-A and HAS-A. Whenever you have one class, which extends another class, it is called as IS-A. Whenever you have a class, in which you are creating object of another class, it is called as HAS-A.
- The keyword 'extends' provides the relationship between two classes.
- To prevent the inheritance, declare the class with the modifier 'final'
- To represent the parent class member, 'super' keyword is used
- To represent the child class member, 'this' keyword is used
- If there is no this or super keyword, then compiler generates super keyword at the first line of constructor
With Extension:
Reusing the similar code again and again
Product
- pid
- pname
- price
LEDTV extends Product
- brand
- technology
Mobile extends Product
- ram
- os
- sdcard
Without Extension:
Repeating the similar code again and again
LEDTV extends Product
- pid
- pname
- price
- brand
- technology
Mobile extends Product
- pid
- pname
- price
- ram
- os
- sdcard
Advantages:
- Code Reusability
- Extensibility
- Overriding
- Data hiding
Example I showing inheritance:
class Teacher{
String designation = "Teacher";
String collegeName = "UH";
void useMarker() {
System.out.println("uses marker to teach");
}
}
public class PhysicsTeacher extends Teacher {
String majorSubject = "Physics";
public static void main(String[] args) {
PhysicsTeacher t = new PhysicsTeacher();
System.out.println(t.collegeName);
System.out.println(t.designation);
System.out.println(t.majorSubject);
t.useMarker();
}
}
Output:
UH
Teacher
Physics
uses marker to teach
In this example, PhysicsTeacher IS-A Teacher. This means that a child class has IS-A relationship with parent class.
Note: If the members or method of parent class is declared as private then the child class can not use them directly. Such private members can only be accessed using public or protected setter and getter methods of super class as shown below example.
Example II:
class Teacher{
private String designation = "Teacher";
private String collegeName = "UH";
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
void useMarker() {
System.out.println("uses marker to teach");
}
}
public class PhysicsTeacher extends Teacher {
String majorSubject = "Physics";
public static void main(String[] args) {
PhysicsTeacher t = new PhysicsTeacher();
t.setCollegeName("UH");
t.setDesignation("Teacher");
System.out.println(t.getCollegeName());
System.out.println(t.getDesignation());
System.out.println(t.majorSubject);
t.useMarker();
}
}
Example III
package com.cerotid.oo.principles;
class Product {
//Attributes (States)
//private int pid;
int pid;
String pname;
int pprice;
//constructor
Product(){
System.out.println("Product object constructed");
}
//Methods
//To write data in Product object
void setProductDetails(int pid, String pname, int pprice) {
this.pid = pid;
this.pname = pname;
this.pprice = pprice;
}
//To read data from Product object
void showProductDetails() {
System.out.println("........Product ID: "+pid+ "...........");
System.out.println("Name: "+pname);
System.out.println("Price: "+pprice);
}
//setter:is a method that updates the value of a variable
void setPid(int pid) {
this.pid = pid;// this means reference to current object
//LHS belongs to the object and RHS belongs to method
}
//getter:is a method that reads a value of variable
int getPid() {
return pid;
}
}
class Mobile extends Product{//IS-A relation, Mobile IS-A Product, Mobile is child, Product is parent
//Additional attributes of Mobile other than Product
String os;
int ram;
int sdCardSize;
Mobile(){
System.out.println("Mobile object constructed");
}
//we have redefined the same method from the parent into the child with different inputs
//we have two methods in the child, one from parent and one in child
//these methods are different based on input even though name is same
//Method overloading: Same method name with different inputs
void setProductDetails(int pid, String pname, int pprice, String os, int ram, int sdCardSize) {
this.pid = pid;
this.pname = pname;
this.pprice = pprice;
this.os = os;
this.ram = ram;
this.sdCardSize = sdCardSize;
System.out.println("Data written in mobile object");
}
//lets refine showProductDetails as well
//but here we have same inputs
//2 methods, 1 from parent and 1 in child and we have same signatures!!
//child methods will be executed and not the parent method
//Method overriding: same method name with same inputs in parent child relationship
void showProductDetails() {
System.out.println("........Product ID: "+pid+ "...........");
System.out.println("Name: "+pname);
System.out.println("Price: "+pprice);
System.out.println("OS: "+os);
System.out.println("RAM: "+ram);
System.out.println("SDCard: "+sdCardSize);
System.out.println("......................");
}
}
public class InheritanceDemo2 {
//main is executed by JVM when my program run
public static void main(String[] args) {
/*
//create an object: Product
Product product1 = new Product();
//product is not an object, its a reference variable which holds the hashCode of the object in hexadecimal notation
System.out.println("Product is: "+product1);
//Writing data in object
product1.setProductDetails(111, "iPhone 6", 720);
//Reading data from object
product1.showProductDetails();
//Lets write the data directly
Product product2 = new Product();
//product2.pid = 222;
product2.setPid(222);
product2.pname = "iPhone 7";
product2.pprice = 800;
//Reading data from object
product2.showProductDetails();
*/
//Requesting to get Mobile Object constructed
Mobile mobile = new Mobile();
//Product object gets constructed before the Mobile object!! this is rule to Inheritance (object to object)
//mobile.setProductDetails(333, "iPhone 8", 9000);
//mobile.showProductDetails();
mobile.setProductDetails(333, "iPhone 8", 900, "iOS", 4, 128);
mobile.showProductDetails();
}
}
No comments:
Post a Comment