OOP with Java - Important Questions & Answers
1. Define Object-Oriented Programming. What are its main principles?
Object-Oriented Programming (OOP) is a paradigm based on 'objects' containing data and methods.
Main Principles:
? Encapsulation ? wrapping data and code into classes and controlling access via modifiers.
? Abstraction ? hiding implementation details and exposing only essential features using abstract classes or
interfaces.
? Inheritance ? acquiring properties/behaviors of a parent class via the extends keyword.
? Polymorphism ? ability of an object to take many forms; achieved by method overloading (compile-time)
and overriding (run-time).
2. Differentiate between Method Overloading and Method Overriding.
Method Overloading ? same method name, different parameter lists; resolved at compile-time; no inheritance
required.
Method Overriding ? same method signature in subclass; resolved at run-time; inheritance mandatory;
enables dynamic binding.
3. What is the difference between an Abstract Class and an Interface?
Abstract Class ? declared with keyword abstract; may contain abstract & non-abstract methods, constructors,
and instance variables; supports single inheritance.
Interface ? declared with keyword interface; specifies a contract; until Java 7 all methods abstract, Java 8+
allows default/static; variables are public static final; supports multiple inheritance of type.
4. Explain Encapsulation with an example.
Encapsulation restricts direct access to fields by marking them private and exposing public getters/setters.
Example:
class Student {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
5. What is inheritance? Explain its types.
Inheritance enables code reuse by letting a subclass derive properties of a superclass.
Types:
? Single ? one child, one parent.
? Multilevel ? chain of inheritance A ? B ? C.
? Hierarchical ? one parent, multiple children.
Page 1
OOP with Java - Important Questions & Answers
? Multiple ? via interfaces only (Java does not allow multiple class inheritance).
6. What is the use of the super keyword in Java?
super refers to the immediate parent.
Uses:
? Access parent?s fields and methods hidden by child.
? Invoke parent constructor from child constructor.
Example:
class B extends A {
B() { super(); }
void show() { System.out.println(super.x); }
}
7. Explain Exception Handling in Java.
Java handles run-time anomalies using:
try { /* risky code */ }
catch(Exception e) { /* handling */ }
finally { /* always executes */ }
throw ? explicitly throw an exception.
throws ? declare exception possibility in method signature.
8. What is multithreading? How can you create a thread?
Multithreading lets a program perform multiple tasks concurrently within a single process.
Create threads by:
? Extending Thread class and overriding run().
? Implementing Runnable and passing instance to a Thread object.
9. Explain Java Collections. Differentiate ArrayList and Vector.
Collections Framework provides reusable data structures.
ArrayList ? not synchronized, faster in single-threaded context.
Vector ? synchronized, thread-safe but slower; grows by doubling size.
10. Describe the JDBC workflow to connect Java with a database.
Steps:
1. Load driver: Class.forName("com.mysql.cj.jdbc.Driver");
2. Obtain connection: Connection con = DriverManager.getConnection(url, user, pass);
3. Create statement: Statement stmt = con.createStatement();
Page 2
OOP with Java - Important Questions & Answers
4. Execute query: ResultSet rs = stmt.executeQuery("SELECT * FROM table");
5. Close connection/resources.
Page 3