Core Java Concepts: Inheritance, Polymorphism & OOP
Q1. Inheritance in Java (10 Marks)
Inheritance is an important feature of object-oriented programming that allows one class to acquire the properties and methods of another class. The class that gives its features is called the parent class or superclass, and the class that receives them is called the child class or subclass. In Java, inheritance is implemented using the extends keyword.
There are three main types of inheritance in Java:
(1) Single-level inheritance – one parent and one child class.
(2) Multilevel inheritance – one class inherits another, and another class further inherits it.
(3) Hierarchical inheritance – one parent class is inherited by multiple child classes.
Java does not support multiple inheritance using classes to avoid the "diamond problem." In a multiple inheritance situation, a child class may inherit two methods with the same signature, which creates ambiguity. To avoid this, Java provides interfaces for achieving multiple inheritance safely.
Example:
class A {
void show() { System.out.println("A class"); }
}
class B extends A {
void display() { System.out.println("B class"); }
}
Inheritance promotes code reusability and supports method overriding, which helps achieve runtime polymorphism.
Q2. Polymorphism in Java (10 Marks)
Polymorphism means "many forms." It allows the same method name to behave differently depending on the situation. Java supports two types of polymorphism: compile-time and runtime.
Compile-time polymorphism (Method Overloading):
When several methods in the same class have the same name but different parameters, it is called method overloading.
Example:
int add(int a, int b)
int add(int a, int b, int c)
Runtime polymorphism (Method Overriding):
When a subclass provides its own implementation of a method already present in the parent class, it is called method overriding.
Example:
class A { void show() {} }
class B extends A { void show() {} }
Difference between Overloading and Overriding:
Overloading occurs at compile time and overriding occurs at runtime. Overloading happens within the same class, while overriding requires inheritance.
Polymorphism makes programs flexible, extensible, and easier to maintain.
Q3. Exception Handling (10 Marks)
Exception handling in Java is a mechanism to handle runtime errors so that the normal flow of the program can continue. Exceptions are unwanted events like divide by zero, file not found, array index out of bounds, etc.
Java provides five important keywords:
try – contains risky code.
catch – handles the exception.
finally – always executes.
throw – used to manually throw an exception.
throws – used in a method declaration to indicate exceptions.
Example:
try {
int a = 10/0;
}
catch(Exception e) {
System.out.println(e);
}
finally {
System.out.println("Finally block executed");
}
Exceptions are classified as:
Checked exceptions like IOException and SQLException, which are checked at compile time.
Unchecked exceptions like ArithmeticException and NullPointerException, which occur at runtime.
Exception handling improves reliability, avoids abnormal program termination, and provides meaningful error messages.
Q4. Collection Framework – ArrayList and LinkedList (10 Marks)
The Java Collection Framework provides ready-made data structures. ArrayList and LinkedList are two important classes under the List interface.
ArrayList uses a dynamic array internally. It allows fast random access using indexes. However, insertion and deletion in the middle are slow because elements must be shifted.
Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
LinkedList uses a doubly linked list internally. It is good for insertion and deletion operations because no shifting is required. However, searching is slower since elements must be traversed.
Example:
LinkedList<String> ll = new LinkedList<>();
ll.add("A");
ll.add("B");
Comparison:
ArrayList is better for searching and accessing elements, while LinkedList is better for frequent insertions and deletions. Both support dynamic sizing and can store heterogeneous objects.
Q5. Threads in Java (10 Marks)
A thread is a lightweight subprocess. Java provides multithreading to perform multiple tasks at the same time. It increases CPU utilization and program efficiency.
There are two methods to create a thread in Java.
1. Extending Thread class:
class A extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
class Test {
public static void main(String[] a) {
A t = new A();
t.start();
}
}
2. Implementing Runnable interface:
class B implements Runnable {
public void run() {
System.out.println("Runnable thread");
}
}
new Thread(new B()).start();
The start() method internally calls the run() method and begins execution in a separate thread. Threads can also be controlled using methods like sleep(), join(), and setPriority().
Q6. JDBC with Connection Steps and CRUD (10 Marks)
JDBC (Java Database Connectivity) is an API used to connect Java applications with databases and perform CRUD operations.
Steps to connect using JDBC:
Load the driver class
Establish connection using
DriverManagerCreate
StatementorPreparedStatementExecute SQL queries
Close the connection
Example:
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement ps = con.prepareStatement("insert into student values(?,?)");
ps.setInt(1, 1);
ps.setString(2, "Ram");
ps.executeUpdate();
CRUD operations include:
INSERT
UPDATE
DELETE
SELECT
JDBC makes database interaction easy and secure.
Q7. Abstract Class vs Interface (10 Marks)
An abstract class contains both abstract and concrete methods. It can have constructors, variables, and non-static methods. It supports single inheritance.
An interface contained only abstract methods before Java 8. From Java 8 onwards, it can also have default and static methods. It supports multiple inheritance because a class can implement multiple interfaces.
Example showing multiple inheritance using interfaces:
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() {}
public void display() {}
}
Interfaces provide abstraction and flexibility, while abstract classes provide partial abstraction.
Q8. super Keyword (10 Marks)
The super keyword refers to the parent class object. It has three main uses in Java.
1. To access parent class variable
2. To call parent class method
3. To call parent class constructor
Example:
class A {
int x = 10;
A() { System.out.println("A constructor"); }
void show() { System.out.println("Parent method"); }
}
class B extends A {
int x = 20;
B() {
super(); // calls parent constructor
}
void display() {
System.out.println(super.x); // parent variable
super.show(); // parent method
}
}
It helps resolve naming conflicts and ensures correct execution of parent class logic.
Q9. Constructor Invocation Sequence (10 Marks)
When inheritance is involved, constructors are always executed from the parent class to the child class. This is because the parent class portion must be created before the child class object is formed.
Example:
class A {
A() { System.out.println("A"); }
}
class B extends A {
B() { System.out.println("B"); }
}
public class Test {
public static void main(String[] args) {
new B();
}
}
Output:
A
B
This shows that the parent class constructor executes first, followed by the child class constructor.
Q10. String Handling in Java (10 Marks)
Java provides many methods to handle and process strings. Strings in Java are immutable, meaning once created they cannot be changed. For modification, Java provides StringBuffer and StringBuilder.
Common string operations:
Comparison:
str1.equals(str2)Length:
str.length()Substring:
str.substring(2,5)Character extraction:
str.charAt(3)
Example:
String s = "Hello";
int l = s.length();
char c = s.charAt(1);
String sub = s.substring(1,4);
StringBuffer is mutable and thread-safe.
Example:
StringBuffer sb = new StringBuffer("Hello");
sb.append(" Java");
String handling is powerful and widely used in Java programming.
English with a size of 10.07 KB