Learning Materials: Object-Oriented Programming (OOP) Concepts in Java
Objective:
Define fundamental OOP concepts such as classes, objects, methods, attributes,
inheritance, polymorphism, encapsulation, and abstraction in Java.
1. Introduction to OOP in Java Object-Oriented Programming (OOP) is a
programming paradigm based on the concept of "objects," which contain data in the
form of fields (attributes) and code in the form of procedures (methods). OOP helps
in organizing code in a modular way, making it more reusable and maintainable.
Key Characteristics of OOP:
Modularity
Reusability
Scalability
Maintainability
2. Fundamental OOP Concepts
a. Class:
A class is a blueprint for creating objects. It defines the attributes and methods that
an object can have.
Example:
class Car {
String brand;
int speed;
void displayInfo() {
System.out.println("Brand: " + brand + ", Speed: " + speed);
}
}
b. Object:
An object is an instance of a class, representing a real-world entity. Example:
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.displayInfo();
c. Methods:
Methods are functions defined within a class to perform actions.
Example:
void accelerate() {
speed += 10;
}
d. Attributes:
Attributes (or fields) store the state of an object.
Example:
String color = "Red";
int speed = 100;
e. Inheritance:
Inheritance allows a class to acquire properties and behaviors of another class,
promoting code reusability.
Example:
class Vehicle {
String type;
}
class Car extends Vehicle {
int wheels;
}
f. Polymorphism:
Polymorphism allows methods to have different implementations based on the
object.
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
g. Encapsulation:
Encapsulation restricts direct access to data by using access modifiers (private,
public, protected).
Example:
class Person {
private String name;
public void setName(String n) { name = n; }
public String getName() { return name; }
}
h. Abstraction:
Abstraction hides complex implementation details and shows only relevant
information.
Example:
abstract class Animal {
abstract void makeSound();
}
class Cat extends Animal {
void makeSound() {
System.out.println("Meow");
}
}
3. Real-Life Analogy of OOP Concepts
Class: Blueprint of a house
Object: A specific house built from the blueprint
Methods: House operations (turning on lights)
Attributes: House characteristics (color, size)
Inheritance: A bungalow inherits characteristics from a generic house
Polymorphism: Different types of houses have different designs
Encapsulation: The wiring inside a house is hidden from users
Abstraction: The control panel of the house exposes essential features only
4. Exercises
1. Create a Java class called Student with attributes name, age, and methods to
display details.
2. Write a program demonstrating inheritance with a Vehicle superclass and a
Bike subclass.
3. Implement polymorphism by overriding a method in a subclass.
5. Summary Understanding OOP concepts is crucial for writing efficient,
maintainable, and scalable Java programs. By mastering classes, objects,
inheritance, polymorphism, encapsulation, and abstraction, students can develop
robust applications.