Introduction to OOP

Java (OOP) Object-Oriented Programming is a programming paradigm centered around objects and data rather than logic and functions. It helps developers create scalable, reusable, and manageable code. OOP revolves around four core principles: Encapsulation, Abstraction, Inheritance, and Polymorphism.

Java OOP

Classes and Objects

Classes act as blueprints for objects. An object is an instance of a class containing both data (attributes) and methods (functions).

Example:

Output: Model: Toyota
Year: 2021

Encapsulation

The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:

  • declare class variables/attributes as private
  • provide public get and set methods to access and update the value of a private variable

Example:

Output:
Student Name: John

Abstraction

Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).

The abstract keyword is a non-access modifier, used for classes and methods:

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Example:

Output: Woof Woof!

Inheritance

In Java, it is possible to inherit attributes and methods from one class to another. We group the “inheritance concept” into two categories:

  • subclass (child) – the class that inherits from another class
  • superclass (parent) – the class being inherited from

To inherit from a class, use the extends keyword.

Example:

Output: Vehicle is starting…
Car is honking: Beep Beep!

Polymorphism

Polymorphism allows methods to perform differently based on the context or object type.

Example:

Output: Sum: 8
Sum: 9.0

Constructors and Overloading

A constructor initializes objects, while overloading allows multiple methods with the same name but different parameters.

Example:

Output: Book Title: Java OOP Guide

Funny and Challenging Practice Questions

  1. Inheritance Madness:
    • Create a class R26BPet. Create subclasses R26BDog and R26BCat. Add a speak() method to each class with different sounds. Instantiate them and call their methods.
  2. Polymorphism Puzzle:
    • Write a program where R26BChef has a cook() method. Overload the method to prepare different dishes when called with various arguments.
  3. Encapsulation Enigma:
    • Create a class R26BWallet. Ensure the balance is private and can only be accessed and modified using proper methods. Try updating the balance incorrectly and handle errors gracefully.
  4. Constructor Chaos:
    • Design a R26BGameCharacter class where the constructor takes the name and initial score. Allow overloaded constructors with different initializations.

Conclusion

Java OOP is a powerful approach to building modular, reusable, and maintainable code. Mastering concepts like classes, objects, encapsulation, abstraction, inheritance, and polymorphism is essential for every Java developer.


Start practicing now by solving the challenging R26B practice problems above and level up your Java skills!

Leave a Reply

Your email address will not be published. Required fields are marked *