Java is an OOP language, which means that Java programs are developed as groups of interrelated objects.
This module introduces you to object-oriented programming (OOP) and the role it plays in Java. OOP concepts such as objects, classes, inheritance, and methods will be discussed. The coverage of each concept is followed up by example Java code. The OOP skills you will learn in this module provide a background that is necessary when it comes to developing effective Java programs.
- Module Learning Objectives
After completing the module, you will have the skills and knowledge necessary to:
- Understand the relationship between classes and objects
- Create simple class definitions
- Add behavior to classes using methods
Object-Oriented Programming (OOP) in Java SE 17 is a fundamental concept that structures programs around objects, which can encapsulate data and behavior. Here is how OOP principles are used in Java SE 17:
- Classes and Objects
- Classes: Define the blueprint or template for objects. A class in Java contains fields (attributes) and methods (behaviors) that represent an objectâs state and behavior.
- Objects: Instances of classes that hold data (attributes) and have methods to perform actions. Objects interact with each other to solve problems and build functionality.
- Encapsulation
- Inheritance
- Polymorphism
- Polymorphism allows one interface to be used for a general class of actions. It is implemented in two ways:
- Compile-time Polymorphism (Method Overloading): Methods in the same class with the same name but different parameter lists.
- Run-time Polymorphism (Method Overriding): Methods in a subclass with the same name and signature as in the superclass, which are overridden to provide specific functionality.
- Example:
// Method Overloading
public class MathUtils {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
// Method Overriding
public class Animal {
public void sound() {
System.out.println("Some generic animal sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
- Abstraction
- Abstraction is the concept of hiding complex implementation details and exposing only the necessary features. This is achieved using:
- Abstract Classes: Classes that cannot be instantiated and may contain abstract (unimplemented) and concrete (implemented) methods.
- Interfaces: Define a contract that implementing classes must follow, using the
interface
keyword.
- Example of an abstract class:
abstract class Vehicle {
abstract void start(); // Abstract method
void stop() {
System.out.println("Vehicle stopped");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car started");
}
}
- Example of an interface:
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
- Interfaces and Functional Programming
- Modules (Introduced in Java 9)
Summary
- Encapsulation: Protects data through access control.
- Inheritance: Facilitates code reuse and a hierarchical relationship.
- Polymorphism: Provides flexibility in method usage and behavior.
- Abstraction: Hides complexity and exposes simple interfaces.
These principles work together to make Java SE 17 a robust and versatile OOP language, enabling developers to create modular, maintainable, and scalable applications.
Objects are key to understanding
object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).
Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions:
- "What possible states can this object be in?" and
- "What possible behavior can this object perform?".
Make sure to write down your observations.
As you do, you will notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors
(turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects.
These real-world observations all translate into the world of object-oriented programming.