OOPS Principles in JAVA
OOPS Principles in JAVA
Introduction
This document provides a set of interview questions and answers on Object-Oriented
Programming (OOP) concepts using Java, targeted at fresh B.Tech students. The aim is to
evaluate their understanding of OOP principles with real-life examples and code snippets.
1. What is a Class?
Question: Can you explain what a class is in OOP?
Answer: A class is a blueprint for creating objects. It defines a datatype by bundling data
(attributes) and methods (functions) that work on the data into a single unit.
Real-life Example: Consider a class Car which defines attributes like color, brand, and
methods like drive(), stop().
void drive() {
System.out.println(brand + " is driving.");
}
void stop() {
System.out.println(brand + " has stopped.");
}
}
2. What is an Object?
Question: What is an object in OOP?
Answer: An object is an instance of a class. It represents a real-world entity with state and
behavior defined by the class.
Follow-up Question: Can you create an object from the Car class and use its methods?
3. What is Encapsulation?
Question: What is encapsulation?
Answer: Encapsulation is the mechanism of wrapping data (variables) and code (methods)
together as a single unit, restricting direct access to some components.
Real-life Example: Think of a capsule that contains medicine inside it, preventing exposure
to its contents.
Answer: Polymorphism means "many shapes". It allows one interface to be used for a
general class of actions, where the specific action is determined by the exact nature of the
situation.
Follow-up Question: Can you demonstrate polymorphism with method overloading and
overriding?
Real-life Example: A person can be a teacher, a father, and a friend in different contexts.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
5. What is Abstraction?
Question: Can you explain abstraction?
Answer: Abstraction is the concept of hiding complex implementation details and showing
only the essential features of an object.
Real-life Example: Using an ATM machine without knowing its internal mechanics.
Answer: Abstraction focuses on hiding the complex implementation details and showing
only the necessary features, while encapsulation is about bundling the data and methods
that operate on the data within a single unit and restricting access to some of the object's
components.
6. What is Inheritance?
Question: What is inheritance in OOP?
Answer: Inheritance is a mechanism where a new class is derived from an existing class,
inheriting its attributes and methods.
Real-life Example: Consider a base class Doctor and derived classes Cardiologist,
Orthopedic, GeneralSurgeon.
class Doctor {
void treatPatient() {
System.out.println("Treating patient");
}
}
7. What is Overloading?
Question: What is method overloading?
Answer: Overloading occurs when two or more methods in the same class have the same
name but different parameters.
8. What is Overriding?
Question: What is method overriding?
Answer: Overriding occurs when a derived class has a definition for one of the member
functions of the base class, replacing the base function.
Follow-up Question: Can you show an example?
class Parent {
void show() {
System.out.println("Parent's show()");
}
}
Answer: An abstract class cannot be instantiated but can be subclassed. It can have
abstract methods and non-abstract methods.
void sleep() {
System.out.println("Animal is sleeping");
}
}
Answer: Use an abstract class when you want to share code among several closely related
classes. Use an interface when you want to specify that a class must implement certain
methods, regardless of where the class is in the inheritance hierarchy.
Answer: An interface is a reference type in Java, similar to a class, that can contain only
abstract methods. A class implements an interface, thereby inheriting the abstract methods
of the interface.
interface Animal {
void eat();
void travel();
}
Vehicle(String type) {
this.type = type;
}
void move() {
System.out.println("Car is moving");
}
}
Shape(String color) {
this.color = color;
}
void draw() {
System.out.println("Drawing a " + color + " circle");
}
}
1. Inheritance: A class can inherit only one abstract class (single inheritance). This is
because Java does not support multiple inheritance with classes.
2. Access Modifiers: Abstract class methods can have any visibility: public, protected,
private.
3. Use Case: Use abstract classes when you have a base class that should provide
some default behavior that subclasses can override or use as-is. It is suitable when
there is a clear hierarchical relationship between classes.
Interface:
1. Implementation: Interfaces cannot have any implementation for methods (prior to
Java 8). From Java 8 onwards, interfaces can have default and static methods with
implementation.
interface Animal {
void sound();
interface Constants {
int MAX_SPEED = 120; // implicitly public, static, and final
}
interface Mammal {
void walk();
}
Summary Table:
Method Implementation Can have both abstract and Can have default and static
concrete methods methods (Java 8+)
Instance Variables Can have instance variables Can only have static final
variables
Access Modifiers Methods can have any Methods are implicitly public
visibility
interface Animal {
void sound();
By understanding the differences between abstract classes and interfaces, you can make
more informed decisions about which to use in your Java programs based on the design
requirements and constraints.