OOP Coding Questions
OOP Coding Questions
Question: Create a Book class with attributes title, author, and price. Implement the
following overloaded constructors:
Solution:
class Book {
String title;
String author;
double price;
Question: Write a Rectangle class that has attributes length and width. Implement multiple
constructors:
Solution:
class Rectangle {
double length;
double width;
Solution:
class BankAccount {
String accountNumber;
String accountHolderName;
double balance;
Question: Create a Computer class that has a Processor class as a member. The Processor
class has attributes brand and speed. The Computer class has attributes brand, model, and a
Processor object.
Solution:
Question: Write a Smartphone class that has a Camera class as a member. The Camera class has
attributes resolution and aperture. The Smartphone class has attributes brand, model, and a
Camera object.
Solution:
Question: Create a House class that contains a Room class. The Room class has attributes
roomType and area. The House class has a HouseName and a list of Room objects.
Solution:
import java.util.ArrayList;
import java.util.List;
Question: Write a Team class and a Player class. The Player class has attributes name and
position. The Team class has a teamName and a list of Player objects.
Solution:
import java.util.ArrayList;
import java.util.List;
// Player class
class Player {
String name;
String position;
Question: Create a City class and a Citizen class. The Citizen class has attributes name and
age. The City class has an attribute cityName and a list of Citizen objects.
Solution:
import java.util.ArrayList;
import java.util.List;
// Citizen class
class Citizen {
String name;
int age;
Question: Design a Department class and an Employee class. The Employee class has attributes
name, ID, and salary. The Department class has a departmentName and a list of Employee
objects.
Solution:
import java.util.ArrayList;
import java.util.List;
// Employee class
class Employee {
String name;
String ID;
double salary;
Question: Create a base class Vehicle with attributes make and year. Derive a class Car that
adds an attribute numberOfDoors. Derive a class Bike that adds an attribute type (e.g., sports,
cruiser).
Solution:
Question: Create a base class Employee with attributes name and employeeID. Derive a class
Manager that adds an attribute department. Derive a class Developer that adds an attribute
programmingLanguage.
Solution:
Question: Create a base class Appliance with attributes brand and power. Derive a class
WashingMachine that adds an attribute loadCapacity. Derive a class Refrigerator that adds
an attribute volume.
Solution:
Question: Create a base class Animal with a method makeSound(). Derive classes Dog and Cat,
and override the makeSound() method for each.
Solution:
Question: Create a base class Shape with a method area(). Derive classes Circle and
Rectangle, and override the area() method for each.
Solution:
@Override
public double area() {
return length * width; // Area of rectangle
}
}
Question: Create a base class Payment with a method processPayment(). Derive classes
CreditCardPayment and CashPayment, and override the processPayment() method for each.
Solution: