The core features that define OOP are:
(Slide 4 showed a diagram with these four features branching from "Object Oriented Programming Features").
Definition: Encapsulation is the bundling of data (attributes) and methods (behavior) that operate on the data within a single unit (a class). It also involves restricting direct access to some of an object's components (data hiding).
private
and providing public
methods (getters and setters) to access or modify the data in a controlled way.(Slide 5 showed an ATM as an analogy for encapsulation. Slide 7 showed a diagram of a capsule containing data (attributes) protected by an outer layer of methods).
Benefits:
private
.public
getter and setter methods for controlled access.File: EncapTest.java
/* File name : EncapTest.java */
public class EncapTest {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}
File: RunEncap.java (Using EncapTest)
/* File name : RunEncap.java */
public class RunEncap {
public static void main(String args[]) {
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
// Output: Name : James Age : 20
}
}
In this example, name
, idNum
, and age
are private. Access is only possible through the public get...()
and set...()
methods.
Definition: Abstraction means hiding the complex implementation details and showing only the essential features (interfaces) of the object to the user. It focuses on *what* an object does, rather than *how* it does it.
Example: Using a mobile phone. You know which buttons to press (interface/abstraction) but don't need to know the complex internal circuitry and software logic (implementation/encapsulation).
(Slide 19 presented a table comparing Abstraction and Encapsulation):
Abstraction | Encapsulation |
---|---|
Solves the problem in the design level. | Solves the problem in the implementation level. |
Used for hiding unwanted data and giving relevant data. | Means hiding the code and data into a single unit to protect the data from outside world. |
Lets you focus on what the object does instead of how it does it. | Means hiding the internal details or mechanics of how an object does something. |
Outer layout, used in terms of design. (Example: Outer look of a Mobile Phone, display screen and keypad buttons) | Inner layout, used in terms of implementation. (Example: Inner implementation detail of a Mobile Phone, how keypad buttons and Display Screen are connected using circuits) |
Definition: Inheritance is a mechanism where a new class (subclass/child class) derives properties (attributes) and behaviors (methods) from an existing class (superclass/parent class).
Terminology:
(Slides 20, 23, 25, 26, 27, 28, 29 showed various diagrams illustrating inheritance: Animal groups, Lion/Cub, Vehicle hierarchy, Ticket hierarchy, Animal/Herbivore/Carnivore etc., Animals/Amphibians/Reptiles/Mammals/Birds).
Code present in a base class need not be rewritten in its child class. Data members and methods of the parent class can be used (unless private) in the child class.
(Slide 25 showed an example where `Confirmed Ticket` and `Requested Ticket` inherit common attributes like Flight Number, Date, Time, Destination from `Air Ticket` and add their own specific attributes like Seat Number or Status).
Definition: Polymorphism (from Greek meaning "many forms") is the ability of an object (or method or operator) to take on many forms. In OOP, it primarily refers to the ability of different objects to respond to the same message (method call) in different ways, specific to their type.
(Slide 32 showed a person taking on different roles: shopper, superhero, presenter, mother, office worker - illustrating the "many forms" concept).
(Slide 33 showed a hierarchy: Shape -> Rectangle, Triangle, Circle).
File: Shape.java (Superclass)
public class Shape {
double dim1;
double dim2;
// These methods might be inherited or used by subclasses
public void SetDim1(double a) {
dim1 = a;
}
public void SetDim2(double b) {
dim2 = b;
}
// Generic area method (to be overridden)
public double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
File: Rectangle.java (Subclass)
public class Rectangle extends Shape {
// Override area for rectangle
@Override // Optional annotation, good practice
public double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2; // Uses inherited dim1, dim2
}
}
File: Triangle.java (Subclass)
public class Triangle extends Shape {
// Constructor (assuming one was needed, added based on slide 36 usage)
public Triangle(double d1, double d2) {
SetDim1(d1);
SetDim2(d2);
}
// Override area for right triangle
@Override // Optional annotation, good practice
public double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2; // Uses inherited dim1, dim2
}
}
File: FindAreas.java (Demonstrating Polymorphism)
public class FindAreas {
public static void main(String args[]) {
Shape f = new Shape(); // Generic Shape object
f.SetDim1(10);
f.SetDim2(10);
Rectangle r = new Rectangle(); // Rectangle object
r.SetDim1(9);
r.SetDim2(5);
Triangle t = new Triangle(10, 8); // Triangle object
// Dimensions set via constructor in this version
// t.SetDim1(10); // These would override constructor values if called
// t.SetDim2(8);
System.out.println("Area is " + r.area()); // Calls Rectangle's area()
// Output: Inside Area for Rectangle.
// Output: Area is 45.0
System.out.println("Area is " + t.area()); // Calls Triangle's area()
// Output: Inside Area for Triangle.
// Output: Area is 40.0
// Note: Calling f.area() would print "Area for Figure is undefined." and return 0.
// Polymorphism example: Using a Shape reference for different objects
Shape shapeRef; // Can refer to Shape, Rectangle, or Triangle
shapeRef = r; // Point to Rectangle
System.out.println("Area via shapeRef (Rectangle): " + shapeRef.area());
// Output: Inside Area for Rectangle.
// Output: Area via shapeRef (Rectangle): 45.0
shapeRef = t; // Point to Triangle
System.out.println("Area via shapeRef (Triangle): " + shapeRef.area());
// Output: Inside Area for Triangle.
// Output: Area via shapeRef (Triangle): 40.0
}
}
The same method call `shapeRef.area()` behaves differently depending on the actual object type (`Rectangle` or `Triangle`) that `shapeRef` points to. This is polymorphism in action.
Besides inheritance (IS-A), other relationships exist:
(Slides 38, 39, 41, 42 showed diagrams illustrating these relationships).
Inheritance can be categorized in different ways:
(Slide 44 showed a diagram like Parent -> Child).
(Slide 45 showed a diagram like Parent1, Parent2 -> Child).
(Slide 47 showed A -> B -> C).
(Slides 30, 49, 50 showed examples: A -> B, A -> C, A -> D; Vehicle -> Land, Water, Air; Land -> Car, Bus, etc.).
(Slide 48 showed A -> B, A -> C, and D inheriting from both B and C).
(Slide 52 showed A -> B, A -> C, and D inheriting from both B and C, creating two paths from A to D).
(Slide 53 showed a general classification diagram).
Inheritance hierarchies represent:
(Slide 51 showed the Vehicle hierarchy with arrows indicating Generalization upwards and Specialization downwards).