Java OOP Concepts - Questions and Answers
1. Question: How would you modify a class with private variables to allow controlled access using
encapsulation principles?
Answer:
Encapsulation is the process of restricting direct access to some of an object's components, which
means data hiding. This is done by declaring variables as private and providing public getter and
setter methods to access and modify those variables.
Key Principles:
- Use private access modifier for variables.
- Use public getter and setter methods.
- Validate data through setters.
- Expose only necessary information.
Example:
class Student {
private String name;
public void setName(String n) { name = n; }
public String getName() { return name; }
2. Question: Write a simple Java code demonstrating encapsulation with a Student class.
Answer:
class Student {
private int age;
public void setAge(int a) { if (a > 0) age = a; }
public int getAge() { return age; }
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setAge(18);
System.out.println("Age: " + s.getAge());
3. Question: Demonstrate method overriding using a base class Shape and subclasses Rectangle
and Circle.
Answer:
class Shape {
void area() { System.out.println("Area of shape"); }
class Rectangle extends Shape {
void area() { System.out.println("Area = length * breadth"); }
class Circle extends Shape {
void area() { System.out.println("Area = pi * r * r"); }
public class Test {
public static void main(String[] args) {
Shape s1 = new Rectangle();
Shape s2 = new Circle();
s1.area();
s2.area();
Explanation:
Each subclass provides its specific implementation of the area() method.
4. Question: Write a Java program demonstrating method overloading with at least two overloaded
methods.
Answer:
class MathUtils {
void add(int a, int b) { System.out.println("Sum: " + (a + b)); }
void add(int a, int b, int c) { System.out.println("Sum: " + (a + b + c)); }
public class Test {
public static void main(String[] args) {
MathUtils m = new MathUtils();
m.add(5, 10);
m.add(1, 2, 3);
5. Question: Create a Student class with multiple constructors to demonstrate constructor
overloading.
Answer:
class Student {
String name;
int age;
Student() { name = "Unknown"; age = 0; }
Student(String n, int a) { name = n; age = a; }
void show() {
System.out.println(name + " - " + age);
6. Question: Implement method overriding using a base class Vehicle and subclasses Car and
Truck.
Answer:
class Vehicle {
void fuelType() { System.out.println("Fuel type: General"); }
}
class Car extends Vehicle {
void fuelType() { System.out.println("Fuel type: Petrol"); }
class Truck extends Vehicle {
void fuelType() { System.out.println("Fuel type: Diesel"); }
7. Question: Demonstrate method overriding and the use of the super keyword with Employee and
Manager classes.
Answer:
class Employee {
void getDetails() { System.out.println("Employee: Rahul"); }
class Manager extends Employee {
void getDetails() {
super.getDetails();
System.out.println("Department: Sales");
8. Question: Demonstrate multiple inheritance using interface Person and Worker.
Answer:
interface Person { void introduce(); }
interface Worker { void work(); }
class Employee implements Person, Worker {
public void introduce() { System.out.println("I am an employee."); }
public void work() { System.out.println("I work in IT."); }
9. Question: Explain how the static keyword optimizes memory usage in a Java class.
Answer:
The static keyword allows memory to be shared across all instances of a class, meaning only one
copy is created in memory, improving efficiency.
Example:
class Student {
String name;
static String school = "ABC School";
Only one copy of 'school' exists in memory for all objects, saving space.