OOP helps organize code by grouping related data and actions together. It makes programs easier to build, understand, and change.
OOP principles overview in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
class ClassName { // fields (data) // methods (actions) }
A class is like a blueprint for objects.
Objects are created from classes and hold actual data.
Examples
Java
class Car { String color; void drive() { System.out.println("Driving"); } }
Java
class Dog { String name; void bark() { System.out.println("Woof!"); } }
Sample Program
This program shows inheritance and method overriding. Dog inherits from Animal and changes how speak() works.
Java
class Animal { String name; void speak() { System.out.println(name + " makes a sound"); } } class Dog extends Animal { @Override void speak() { System.out.println(name + " says Woof!"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.name = "Buddy"; dog.speak(); } }
Important Notes
Encapsulation means keeping data safe inside classes.
Inheritance lets one class use features of another.
Polymorphism means one action can work in different ways.
Abstraction hides complex details and shows only what is needed.
Summary
OOP groups data and actions into classes and objects.
Four main principles: Encapsulation, Inheritance, Polymorphism, Abstraction.
OOP makes code easier to manage and grow.
Practice
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
Solution
Step 1: Recall the four main OOP principles
The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.Step 2: Identify the option not in the list
Compilation is a process of converting code to machine language, not an OOP principle.Final Answer:
Compilation -> Option DQuick Check:
OOP principles exclude Compilation [OK]
Hint: Remember the four OOP pillars: E, I, P, A [OK]
Common Mistakes:
- Confusing compilation with abstraction
- Mixing OOP principles with programming processes
2. Which Java keyword is used to inherit properties from a parent class?
easy
Solution
Step 1: Identify the keyword for class inheritance in Java
Java uses the keywordextendsto inherit from a parent class.Step 2: Differentiate from other keywords
implementsis for interfaces,superaccesses parent members, andinheritsis not a Java keyword.Final Answer:
extends -> Option CQuick Check:
Inheritance keyword = extends [OK]
Hint: Use 'extends' to inherit classes in Java [OK]
Common Mistakes:
- Using 'implements' for class inheritance
- Confusing 'super' with inheritance keyword
3. What will be the output of the following Java code?
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}medium
Solution
Step 1: Understand method overriding and polymorphism
The objectais declared as Animal but refers to a Dog instance. Thesound()method is overridden in Dog.Step 2: Determine which method runs at runtime
Java uses dynamic method dispatch, so the Dog'ssound()method runs, printing "Bark".Final Answer:
Bark -> Option AQuick Check:
Overridden method runs = Bark [OK]
Hint: Overridden methods run from actual object type [OK]
Common Mistakes:
- Thinking declared type method runs
- Expecting compilation or runtime errors
4. Identify the error in the following Java code snippet:
class Vehicle {
private int speed;
public int getSpeed() { return speed; }
}
class Car extends Vehicle {
public void setSpeed(int speed) { this.speed = speed; }
}medium
Solution
Step 1: Check access to private fields in subclass
The fieldspeedis private in Vehicle, so Car cannot access it directly.Step 2: Analyze the setSpeed method in Car
Car'ssetSpeedtries to assignthis.speed, which is not accessible, causing a compile error.Final Answer:
Cannot access private field 'speed' directly in subclass -> Option AQuick Check:
Private fields inaccessible in subclass [OK]
Hint: Private fields are not visible in subclasses [OK]
Common Mistakes:
- Assuming private fields are inherited
- Ignoring access modifiers
5. You want to design a Java class hierarchy where different shapes (Circle, Rectangle) can calculate their area. Which OOP principle best supports writing a method
double getArea() in a base class or interface and having each shape provide its own implementation?hard
Solution
Step 1: Understand the scenario of method overriding
Each shape class provides its own version ofgetArea(), meaning the method behaves differently depending on the object.Step 2: Identify the OOP principle for multiple forms of a method
This is Polymorphism, where the same method name works differently in subclasses.Final Answer:
Polymorphism -> Option BQuick Check:
Different behaviors for same method = Polymorphism [OK]
Hint: Same method, different behaviors = Polymorphism [OK]
Common Mistakes:
- Confusing abstraction with polymorphism
- Thinking inheritance alone handles this
