OOP Questions and Answers with Java Codes
Topics: Encapsulation, Abstract Class, Interface, Cloning, Marker Interface
Question No: 05
Why is encapsulation important in object-oriented programming? Explain how declaring
data fields as private and using getter/setter methods improves data security,
maintainability, and control over object state.
Answer:
Encapsulation is one of the main concepts of Object-Oriented Programming (OOP). It means
hiding data inside a class and controlling access to that data using methods. In
encapsulation, variables are declared as private and accessed using getter and setter
methods.
Importance of Encapsulation
Data Security: Private variables cannot be accessed directly from outside the class. This
protects data from unauthorized changes.
Better Control: Setter methods allow validation before changing data.
Maintainability: Code becomes easier to maintain because internal implementation
can change without affecting other classes.
Flexibility: Validation rules can easily be added inside methods.
Example Code:
class Account {
private double balance;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
if(balance >= 0) {
[Link] = balance;
}
}
}
Question No: 06
Compare abstract classes and interfaces in Java. Discuss instantiation, abstract methods,
data fields, implementation responsibility of subclasses, and when an interface is more
suitable than an abstract class.
Answer:
Abstract Class Interface
Uses abstract class keyword Uses interface keyword
Cannot create objects directly Cannot create objects directly
Can contain abstract and concrete methods Mostly contains abstract methods
Can have constructors Cannot have constructors
Can contain normal variables Variables are public static final
Uses extends Uses implements
Abstract Class Example:
abstract class Animal {
abstract void sound();
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
Interface Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("Dog barks");
}
}
Question No: 07
Differentiate between shallow copy and deep copy in object cloning. What is a marker
interface? Explain the purpose of the Cloneable interface.
Answer:
Shallow Copy Deep Copy
Copies reference only Copies actual object
Same memory is shared Separate memory is created
Changes affect both objects Changes do not affect each other
Faster Slower
A marker interface is an interface with no methods or variables. It gives special information
to the JVM.
Cloneable is a marker interface used to allow object cloning using the clone() method.
Cloneable Example:
class Student implements Cloneable {
int id = 10;
public static void main(String[] args) throws Exception {
Student s1 = new Student();
Student s2 = (Student)[Link]();
[Link]([Link]);
}
}
Question No: 08
Can an abstract class contain both abstract and concrete methods? Explain with an example
idea.
Answer:
Yes. An abstract class can contain both abstract methods and concrete methods. Abstract
methods do not have a body, while concrete methods contain complete code.
Example Code:
abstract class Shape {
// Abstract method
abstract void area();
// Concrete method
void display() {
[Link]("Shape Class");
}
}
class Circle extends Shape {
void area() {
[Link]("Area of Circle");
}
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
[Link]();
[Link]();
}
}
Conclusion
These OOP concepts are important in Java programming because they improve security,
code reusability, abstraction, maintainability, and flexibility.