1.
Simple Inheritance
Question: Create a class Animal with a method makeSound() and a subclass Dog that
overrides this method to print "Bark".
java
Copy code
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
}
}
2. Constructor Chaining
Question: Create a class Person with a constructor that accepts a name, and a
subclass Employee with a constructor that also accepts salary. Use super() to
initialize the parent class.
java
Copy code
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class Employee extends Person {
double salary;
Employee(String name, double salary) {
super(name);
this.salary = salary;
}
void display() {
System.out.println("Name: " + name + ", Salary: " + salary);
}
public static void main(String[] args) {
Employee emp = new Employee("John", 50000);
emp.display();
}
}
3. Method Overloading (Compile-time Polymorphism)
Question: Write a class Calculator with two add methods: one that adds two
integers, and one that adds three integers.
java
Copy code
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20));
System.out.println(calc.add(5, 10, 15));
}
}
4. Method Overriding (Run-time Polymorphism)
Question: Define a superclass Shape with a method draw(). Then define Circle and
Rectangle classes that override draw() with their own implementation.
java
Copy code
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle extends Shape {
@Override
void draw() {
System.out.println("Drawing a rectangle");
}
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
shape = new Rectangle();
shape.draw();
}
}
5. Super Keyword Usage
Question: In a class hierarchy Vehicle → Car, override a method display() in Car
and use super.display() to call the parent method.
java
Copy code
class Vehicle {
void display() {
System.out.println("This is a Vehicle");
}
}
class Car extends Vehicle {
@Override
void display() {
super.display();
System.out.println("This is a Car");
}
public static void main(String[] args) {
Car car = new Car();
car.display();
}
}
6. Constructor Overloading
Question: Create a class Student with multiple constructors:
One with no parameters
One with a String name
One with String name and int age
java
Copy code
class Student {
String name;
int age;
Student() {
this.name = "Unknown";
this.age = 0;
}
Student(String name) {
this.name = name;
this.age = 0;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Alice");
Student s3 = new Student("Bob", 22);
s1.display();
s2.display();
s3.display();
}
}
7. Dynamic Method Dispatch
Question: Declare a reference of type Animal, assign it an object of type Cat, and
call the method speak(). Demonstrate dynamic dispatch.
java
Copy code
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Cat extends Animal {
@Override
void speak() {
System.out.println("Meow");
}
public static void main(String[] args) {
Animal animal = new Cat();
animal.speak(); // Output: Meow
}
}
8. Abstract Class Example
Question: Create an abstract class Appliance with an abstract method turnOn().
Implement it in a subclass Fan.
java
Copy code
abstract class Appliance {
abstract void turnOn();
}
class Fan extends Appliance {
@Override
void turnOn() {
System.out.println("Fan is turned on");
}
public static void main(String[] args) {
Fan fan = new Fan();
fan.turnOn();
}
}
9. Encapsulation
Question: Create a class Account with a private field balance. Provide methods
deposit() and getBalance().
java
Copy code
class Account {
private double balance = 0;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(1000);
System.out.println("Balance: " + acc.getBalance());
}
}
10. Simple Aggregation
Question: Create a class Library that has a Book object as a member. Show how
aggregation works.
java
Copy code
class Book {
String title;
Book(String title) {
this.title = title;
}
void showTitle() {
System.out.println("Book: " + title);
}
}
class Library {
Book book;
Library(Book book) {
this.book = book;
}
void showBook() {
book.showTitle();
}
public static void main(String[] args) {
Book b = new Book("Java Programming");
Library lib = new Library(b);
lib.showBook();
}
}
End of Document. These examples cover key Java OOP principles: inheritance,
polymorphism, encapsulation, abstraction, and more.