Bird
Raised Fist0
Javaprogramming~10 mins

OOP principles overview in Java - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - OOP principles overview
Start
Encapsulation
Inheritance
Polymorphism
Abstraction
End
This flow shows the four main OOP principles in order: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Execution Sample
Java
class Animal {
  void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
  void sound() { System.out.println("Bark"); }
}
public class Main {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound();
  }
}
This code shows inheritance and polymorphism: Dog inherits Animal and overrides sound(), then Dog's sound() is called via Animal reference.
Execution Table
StepActionObject TypeMethod CalledOutput
1Create Dog object assigned to Animal referenceDognonenone
2Call sound() on Animal referenceDogsound()Bark
3End of main methodnonenonenone
💡 Program ends after calling overridden method sound() on Dog object
Variable Tracker
VariableStartAfter Step 1After Step 2Final
anullDog object referenceDog object referenceDog object reference
Key Moments - 3 Insights
Why does calling sound() on Animal reference print "Bark" instead of "Some sound"?
Because the actual object is Dog, and Dog overrides sound(). Java uses the object's method (Dog's) at runtime, shown in execution_table step 2.
What is encapsulation in OOP?
Encapsulation means keeping data and methods inside a class private and safe, so other parts of the program can't change them directly.
How is abstraction different from encapsulation?
Abstraction hides complex details and shows only what is needed, while encapsulation hides data inside the class to protect it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type is the object referenced by 'a' after step 1?
AAnimal
BMain
CDog
DObject
💡 Hint
Check execution_table row 1 under 'Object Type' column
At which step does the overridden method sound() get called?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at execution_table row 2 under 'Method Called' and 'Output'
If the Dog class did not override sound(), what would be the output at step 2?
A"Some sound"
B"Bark"
CNo output
DCompilation error
💡 Hint
Think about method overriding and what happens if no override exists
Concept Snapshot
OOP Principles in Java:
- Encapsulation: Hide data inside classes
- Inheritance: Child classes reuse parent code
- Polymorphism: Same method acts differently
- Abstraction: Show only needed details
Use classes and methods to organize code clearly.
Full Transcript
This visual execution shows the four main OOP principles: Encapsulation, Inheritance, Polymorphism, and Abstraction. The sample Java code creates a Dog object assigned to an Animal reference. When calling sound() on this reference, Dog's overridden method runs, printing "Bark". Variables and method calls are tracked step-by-step. Key moments clarify why the overridden method runs and the difference between encapsulation and abstraction. The quiz tests understanding of object types, method calls, and overriding behavior.

Practice

(1/5)
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Compilation

Solution

  1. Step 1: Recall the four main OOP principles

    The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.
  2. Step 2: Identify the option not in the list

    Compilation is a process of converting code to machine language, not an OOP principle.
  3. Final Answer:

    Compilation -> Option D
  4. Quick 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
A. implements
B. inherits
C. extends
D. super

Solution

  1. Step 1: Identify the keyword for class inheritance in Java

    Java uses the keyword extends to inherit from a parent class.
  2. Step 2: Differentiate from other keywords

    implements is for interfaces, super accesses parent members, and inherits is not a Java keyword.
  3. Final Answer:

    extends -> Option C
  4. Quick 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
A. Bark
B. Animal sound
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand method overriding and polymorphism

    The object a is declared as Animal but refers to a Dog instance. The sound() method is overridden in Dog.
  2. Step 2: Determine which method runs at runtime

    Java uses dynamic method dispatch, so the Dog's sound() method runs, printing "Bark".
  3. Final Answer:

    Bark -> Option A
  4. Quick 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
A. Cannot access private field 'speed' directly in subclass
B. Cannot override getSpeed() method
C. Missing constructor in Car class
D. No error, code is correct

Solution

  1. Step 1: Check access to private fields in subclass

    The field speed is private in Vehicle, so Car cannot access it directly.
  2. Step 2: Analyze the setSpeed method in Car

    Car's setSpeed tries to assign this.speed, which is not accessible, causing a compile error.
  3. Final Answer:

    Cannot access private field 'speed' directly in subclass -> Option A
  4. Quick 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
A. Encapsulation
B. Polymorphism
C. Abstraction
D. Inheritance

Solution

  1. Step 1: Understand the scenario of method overriding

    Each shape class provides its own version of getArea(), meaning the method behaves differently depending on the object.
  2. Step 2: Identify the OOP principle for multiple forms of a method

    This is Polymorphism, where the same method name works differently in subclasses.
  3. Final Answer:

    Polymorphism -> Option B
  4. Quick 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