0% found this document useful (0 votes)
6 views4 pages

Java OOP Interview Programs Explained

The document presents various Java OOP concepts through interview programs, including method overloading and overriding, abstraction with interfaces and abstract classes, inheritance, the singleton design pattern, and custom exceptions. Each section includes example code demonstrating the concepts and expected output. The examples illustrate key principles of object-oriented programming in Java, such as code reusability and exception handling.

Uploaded by

Jaya Ram
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java OOP Interview Programs Explained

The document presents various Java OOP concepts through interview programs, including method overloading and overriding, abstraction with interfaces and abstract classes, inheritance, the singleton design pattern, and custom exceptions. Each section includes example code demonstrating the concepts and expected output. The examples illustrate key principles of object-oriented programming in Java, such as code reusability and exception handling.

Uploaded by

Jaya Ram
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java OOP Concepts - Interview Programs

1. Method Overloading and Overriding

Demonstrates method overloading (same method name, different parameters) and overriding (subclass

modifies parent method).

class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
class AdvancedCalculator extends Calculator {
@Override
int add(int a, int b) {
return a + b + 10;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Overloading: " + [Link](5, 10));
[Link]("Overloading: " + [Link](2.5, 3.5));
Calculator adv = new AdvancedCalculator();
[Link]("Overriding: " + [Link](5, 10));
}
}

Expected Output:

Overloading: 15

Overloading: 6.0

Overriding: 25

2. Abstraction with Interface and Abstract Class

Uses both interface and abstract class to demonstrate abstraction and code reusability.

interface Engine {
void start();
}
abstract class Vehicle {
Java OOP Concepts - Interview Programs

abstract void fuelType();


void wheels() {
[Link]("Has 4 wheels");
}
}
class Car extends Vehicle implements Engine {
public void start() {
[Link]("Car engine started");
}
void fuelType() {
[Link]("Petrol/Diesel");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
[Link]();
[Link]();
[Link]();
}
}

Expected Output:

Car engine started

Petrol/Diesel

Has 4 wheels

3. Inheritance (Single and Multilevel)

Demonstrates both single and multilevel inheritance where classes derive behavior from each other.

class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Puppy extends Dog {
Java OOP Concepts - Interview Programs

void weep() {
[Link]("Puppy weeps");
}
}
public class Main {
public static void main(String[] args) {
Puppy puppy = new Puppy();
[Link]();
[Link]();
[Link]();
}
}

Expected Output:

Animal makes a sound

Dog barks

Puppy weeps

4. Singleton Design Pattern

Ensures only one instance of a class is created during the lifetime of the program.

class Singleton {
private static Singleton instance;
private Singleton() {
[Link]("Instance created");
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton s1 = [Link]();
Singleton s2 = [Link]();
[Link](s1 == s2);
}
}
Java OOP Concepts - Interview Programs

Expected Output:

Instance created

true

5. Custom Exception Example

Shows how to define and use a user-defined exception class in Java.

class InvalidAgeException extends Exception {


public InvalidAgeException(String message) {
super(message);
}
}
public class Main {
static void checkAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age is less than 18!");
} else {
[Link]("Eligible to vote");
}
}
public static void main(String[] args) {
try {
checkAge(16);
} catch (InvalidAgeException e) {
[Link]("Caught exception: " + [Link]());
}
}
}

Expected Output:

Caught exception: Age is less than 18!

You might also like