Back to Java Programming
Java Programming

OOP in Java

Classes, objects, constructors, inheritance, method overriding, and polymorphism in Java.

BeginnerIntermediateAdvanced

What You Will Learn in OOP in Java

Object-Oriented Programming in Java organises code as objects (instances of classes) that encapsulate data and behaviour, supporting inheritance, polymorphism, and abstraction.

  • Four pillars: Encapsulation, Inheritance, Polymorphism, Abstraction.
  • Class = blueprint; Object = instance with actual memory allocation.
  • Constructor: special method with same name as class, no return type, initialises objects.
  • Inheritance: `extends` keyword; Java supports single class inheritance, multiple interface inheritance.
  • Polymorphism: compile-time (method overloading) and runtime (method overriding).
  • `super` refers to parent class; `this` refers to current instance.

Syntax

class Parent {
    void display() { System.out.println("Parent"); }
}
class Child extends Parent {
    @Override
    void display() { System.out.println("Child"); } // runtime polymorphism
    void display(String msg) { System.out.println(msg); } // overloading
}

Complete Code Example

class Animal {
    String name;
    Animal(String name) { this.name = name; }
    void sound() { System.out.println(name + " makes a sound"); }
}
class Dog extends Animal {
    Dog(String name) { super(name); }
    @Override
    void sound() { System.out.println(name + " barks"); }
}
public class Main {
    public static void main(String[] args) {
        Animal a = new Dog("Rex"); // upcasting
        a.sound(); // Output: Rex barks  (runtime polymorphism)
    }
}

Example

A `Dog` class extending `Animal` and overriding `sound()` demonstrates runtime polymorphism.

Expected Exam Questions — OOP in Java

Q1.What is encapsulation? How is it achieved in Java?
Answer: Encapsulation wraps data (fields) and methods together in a class, hiding internal state from outside. It is achieved by declaring fields as `private` and providing `public` getter/setter methods.
Q2.What is the difference between method overloading and overriding?
Answer: Overloading: same method name, different parameters, same class — resolved at compile time. Overriding: child class redefines parent's method with same signature — resolved at runtime via dynamic dispatch.
Q3.What is an abstract class? How does it differ from an interface?
Answer: Abstract class: can have both abstract (no body) and concrete methods, can have constructors, supports single inheritance. Interface: all methods are abstract by default (Java 7), supports multiple inheritance. Use abstract class for IS-A with shared code; interface for CAN-DO contracts.
Q4.What is the difference between `IS-A` and `HAS-A` relationships?
Answer: IS-A (inheritance): `Dog extends Animal` — Dog IS-A Animal. HAS-A (composition/aggregation): `Car has Engine` — Car HAS-A Engine object as a field. Favour composition over inheritance for looser coupling.
Q5.Can a constructor be inherited in Java?
Answer: No. Constructors are not inherited. However, `super()` in a child class constructor calls the parent constructor. If not written explicitly, Java implicitly calls `super()` (no-arg parent constructor).

🔘 MCQ Practice — OOP in Java

MCQ 1.Which OOP concept is demonstrated when the same method behaves differently in different objects?
A. Encapsulation
B. Abstraction
C. Polymorphism
D. Inheritance

✓ Correct Answer: Polymorphism

MCQ 2.A class cannot extend more than one class in Java. This is a limitation of:
A. Multiple Inheritance
B. Abstraction
C. Polymorphism
D. Encapsulation

✓ Correct Answer: Multiple Inheritance

MCQ 3.Which keyword is used to call a parent class constructor?
A. this()
B. parent()
C. super()
D. base()

✓ Correct Answer: super()

Download OOP in Java PDF Notes

Get the complete OOP in Java notes as a PDF — free for enrolled students, or browse our public study materials library.

More Java Programming Topics

Related Subjects

Frequently Asked Questions — OOP in Java

What is OOP in Java in Java Programming?
Object-Oriented Programming in Java organises code as objects (instances of classes) that encapsulate data and behaviour, supporting inheritance, polymorphism, and abstraction.
What is encapsulation? How is it achieved in Java?
Encapsulation wraps data (fields) and methods together in a class, hiding internal state from outside. It is achieved by declaring fields as `private` and providing `public` getter/setter methods.
What is the difference between method overloading and overriding?
Overloading: same method name, different parameters, same class — resolved at compile time. Overriding: child class redefines parent's method with same signature — resolved at runtime via dynamic dispatch.
What is an abstract class? How does it differ from an interface?
Abstract class: can have both abstract (no body) and concrete methods, can have constructors, supports single inheritance. Interface: all methods are abstract by default (Java 7), supports multiple inheritance. Use abstract class for IS-A with shared code; interface for CAN-DO contracts.
What is the difference between `IS-A` and `HAS-A` relationships?
IS-A (inheritance): `Dog extends Animal` — Dog IS-A Animal. HAS-A (composition/aggregation): `Car has Engine` — Car HAS-A Engine object as a field. Favour composition over inheritance for looser coupling.
Can a constructor be inherited in Java?
No. Constructors are not inherited. However, `super()` in a child class constructor calls the parent constructor. If not written explicitly, Java implicitly calls `super()` (no-arg parent constructor).
How do I prepare OOP in Java for exams?
To master OOP in Java, start by reading the theory carefully, then go through solved examples step by step. Practice numericals (if applicable), revise key formulas, and attempt previous year questions. SII notes cover all these aspects in a structured manner.
Are these OOP in Java notes free?
Yes! SII provides free access to OOP in Java notes and introductory study materials. Enrolled students get full access to detailed notes, solved papers, and live doubt-clearing sessions.
Which exams ask questions from OOP in Java?
OOP in Java is an important topic tested in Beginner, Intermediate, Advanced board exams, as well as GATE (CS & IT), University Semester Exams. It frequently appears in both short-answer and long-answer sections.