![]() |
Java Programming for Beginners: Chapter 5 - Object-Oriented Programming in Java |
In this chapter, we'll explore the fundamental concepts of object-oriented programming (OOP) in Java. Object-oriented programming is a programming paradigm based on the concept of "objects," which can contain data and code to manipulate that data.
1. Classes and Objects
s is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have.
Copy code
public class Car { // Attributes String make; String model; int year; // Constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } // Method public void displayInfo() { System.out.println("Make: " + make); System.out.println("Model: " + model); System.out.println("Year: " + year); } } // Creating an object of the Car class Car myCar = new Car("Toyota", "Camry", 2022); // Accessing object properties and methods myCar.displayInfo();
In this example, we define a Car class with attributes make, model, and year, a constructor to initialize those attributes, and a method displayInfo() to print the car's information.
2. Inheritance
Inheritance is a mechanism in OOP that allows a class to inherit properties and behaviors from another class, called the superclass or parent class.
Copy code
public class Vehicle { String make; String model; public Vehicle(String make, String model) { this.make = make; this.model = model; } public void displayInfo() { System.out.println("Make: " + make); System.out.println("Model: " + model); } } // Car class inheriting from Vehicle class public class Car extends Vehicle { int year; public Car(String make, String model, int year) { super(make, model); this.year = year; } public void displayYear() { System.out.println("Year: " + year); } } // Creating an object of the Car class Car myCar = new Car("Toyota", "Camry", 2022); // Accessing inherited properties and methods myCar.displayInfo(); myCar.displayYear();
In this example, the Car class inherits the make and model properties and the displayInfo() method from the Vehicle class.
3. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables flexibility and extensibility in the design of object-oriented systems.
Copy code
// Vehicle class public class Vehicle { public void drive() { System.out.println("Driving a vehicle"); } } // Car class public class Car extends Vehicle { @Override public void drive() { System.out.println("Driving a car"); } } // Truck class public class Truck extends Vehicle { @Override public void drive() { System.out.println("Driving a truck"); } } // Polymorphism in action Vehicle car = new Car(); Vehicle truck = new Truck(); car.drive(); // Output: Driving a car truck.drive(); // Output:
Driving a truck
In this example, the drive() method is overridden in both the Car and Truck classes, providing different implementations for each subclass. When we call the drive() method on objects of type Vehicle, the appropriate implementation is invoked based on the actual type of the object.
Conclusion
Object-oriented programming is a powerful paradigm for building complex and maintainable software systems. By understanding the concepts of classes, objects, inheritance, and polymorphism, you can create robust and flexible Java applications. Practice implementing and using these concepts in your programs to become proficient in object-oriented programming.