Object-Oriented Programming (OOP) Concepts in Java - Stanford University - 2025
Object-Oriented Programming (OOP) Concepts in Java - Stanford University - 2025
Introduction
Object-Oriented Programming (OOP) is a paradigm in software development that focuses on creating objects to represent real-world entities. Java, being one of the most popular programming languages, is built around the OOP concept, making it essential for Java developers to master these principles. This guide will introduce the fundamental OOP concepts in Java, including classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
Why OOP in Java?
Java’s implementation of OOP offers a structured approach to building modular, scalable, and reusable code. By learning OOP in Java, developers can:
- Improve code organization: Code is divided into logical structures (classes) representing entities and their behaviors.
- Reuse code efficiently: OOP supports reusability through inheritance and polymorphism, reducing redundancy.
- Enhance maintainability: Well-structured OOP code is easier to debug and maintain.
- Model real-world problems: OOP allows developers to design systems based on real-world entities, improving clarity and understanding.
Core OOP Concepts in Java
- Classes and Objects
At the heart of OOP are classes and objects. A class is a blueprint that defines the attributes (fields) and behaviors (methods) of an object. An object is an instance of a class.
class Car {
String model;
int year;
void startEngine() {
System.out.println("Engine started.");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car(); // Creating an object
car.model = "Tesla Model 3";
car.year = 2020;
car.startEngine(); // Calling a method
}
}
In this example, Car
is a class, and car
is an object of that class. The object holds the model and year of the car, and it can perform the action of starting the engine through the startEngine()
method.
- Inheritance
Inheritance is one of the fundamental OOP concepts that allows a class to inherit properties and methods from another class. The class that inherits is called the subclass, and the class it inherits from is called the superclass.
class Vehicle {
String type;
void move() {
System.out.println("The vehicle is moving.");
}
}
class Car extends Vehicle {
String model;
void displayModel() {
System.out.println("Car model: " + model);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.type = "Sedan";
car.model = "Honda Civic";
car.move(); // Inherited method from Vehicle
car.displayModel();
}
}
In this example, Car
inherits from Vehicle
. The Car
class gains access to the move()
method from Vehicle
, showcasing the power of inheritance in reducing code duplication.
- **Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables one interface to be used for different data types or classes, enhancing flexibility and scalability.
Polymorphism is implemented in two ways:
- Compile-time Polymorphism (Method Overloading)
- Runtime Polymorphism (Method Overriding)
Method Overloading:
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls first method
System.out.println(calc.add(5, 10, 15)); // Calls overloaded method
}
}
Method Overriding:
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog(); // Runtime polymorphism
myDog.sound(); // Calls the overridden method in Dog
}
}
In the above example, Dog
overrides the sound()
method from Animal
, demonstrating runtime polymorphism.
4. Encapsulation
Encapsulation is the practice of wrapping data (fields) and methods into a single unit (class) and restricting access to some of the object's components. This is typically achieved using access modifiers like private
, protected
, and public
.
class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(500);
System.out.println("Balance: " + account.getBalance());
}
}
In this example, the balance
field is private, and it can only be accessed through public getter and setter methods, ensuring control over the data.
- Abstraction
Abstraction focuses on hiding the complexity of a system and exposing only the necessary parts. This can be achieved using abstract classes and interfaces in Java.
Abstract Class:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw(); // Calls the draw() method implemented in Circle
}
}
In this example, Shape
is an abstract class that defines the abstract method draw()
. The Circle
class provides the concrete implementation of this method.
Conclusion
Object-Oriented Programming in Java provides a powerful framework for writing clean, modular, and reusable code. By understanding and applying concepts like classes, inheritance, polymorphism, encapsulation, and abstraction, developers can design robust software systems that are easier to maintain and extend. OOP is a foundational skill in software development, and mastering it in Java is essential for anyone looking to excel in programming.
keywords: Object-Oriented Programming, OOP in Java, Java Programming, Inheritance in Java, Polymorphism, Encapsulation, Abstraction, Classes and Objects, Stanford University 2024, Java OOP Guide, Learn Java OOP, Method Overloading, Method Overriding