0% found this document useful (0 votes)
4 views13 pages

Oops With Jaava

The document explains the four fundamental principles of Object-Oriented Programming (OOP): Encapsulation, Abstraction, Inheritance, and Polymorphism, providing Java examples for each. It also discusses inheritance types, exception handling, the use of keywords like super and final, and the differences between various Java collections and concepts such as ArrayList vs LinkedList, HashMap vs TreeMap, and method overloading vs overriding. Additionally, it covers constructors, abstract classes, interfaces, and the this keyword in Java.

Uploaded by

sameerrizvi322
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)
4 views13 pages

Oops With Jaava

The document explains the four fundamental principles of Object-Oriented Programming (OOP): Encapsulation, Abstraction, Inheritance, and Polymorphism, providing Java examples for each. It also discusses inheritance types, exception handling, the use of keywords like super and final, and the differences between various Java collections and concepts such as ArrayList vs LinkedList, HashMap vs TreeMap, and method overloading vs overriding. Additionally, it covers constructors, abstract classes, interfaces, and the this keyword in Java.

Uploaded by

sameerrizvi322
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
You are on page 1/ 13

1.

Explain the four fundamental principles of Object-Oriented


Programming (OOP) with suitable examples.

Answer:​
The four fundamental principles of Object-Oriented Programming (OOP) are:

Encapsulation:​
Encapsulation refers to bundling the data (attributes) and the methods (functions) that
operate on the data into a single unit (class). This helps in hiding the internal details of the
object from the outside world. In Java, we achieve encapsulation using access modifiers
(private, public, etc.).​

java​
CopyEdit​
class Car {
private String model;
private int year;

// Setter methods (Encapsulation)


public void setModel(String model) { this.model = model; }
public void setYear(int year) { this.year = year; }

// Getter methods (Encapsulation)


public String getModel() { return model; }
public int getYear() { return year; }
}

●​

Abstraction:​
Abstraction means hiding complex implementation details and showing only the essential
features of an object. In Java, abstraction can be achieved using abstract classes or
interfaces.​

java​
CopyEdit​
abstract class Animal {
abstract void sound(); // Abstract method
}

class Dog extends Animal {


void sound() {
System.out.println("Bark"); // Concrete method
implementation
}
}

●​

Inheritance:​
Inheritance allows one class (subclass) to inherit properties and behaviors (methods) from
another class (superclass). It promotes reusability of code.​

java​
CopyEdit​
class Animal {
void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal { // Dog inherits Animal


void bark() { System.out.println("Dog barks"); }
}

●​
●​ Polymorphism:​
Polymorphism allows objects to take many forms. It is achieved by overriding or
overloading methods.​

○​ Method Overloading: Same method name, but with different parameters.​

java​
CopyEdit​
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}

●​
○​ Method Overriding: A subclass provides its own implementation of a method
already defined in its superclass.​

java​
CopyEdit​
class Animal {
void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {


@Override
void sound() { System.out.println("Bark"); }
}

●​

2. What is Inheritance in Java? Explain its types with examples. How


does inheritance promote code reusability?

Answer:​
Inheritance is a mechanism in which one class (child class) acquires the properties and
behaviors (methods) of another class (parent class). It helps in the reuse of code, thereby
reducing redundancy.

Single Inheritance: A class inherits from one superclass.​



java​
CopyEdit​
class Animal {
void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal {


void bark() { System.out.println("Dog barks"); }
}

●​

Multilevel Inheritance: A class inherits from another subclass.​



java​
CopyEdit​
class Animal {
void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal {


void bark() { System.out.println("Dog barks"); }
}

class Bulldog extends Dog {


void run() { System.out.println("Bulldog runs"); }
}
●​

Hierarchical Inheritance: Multiple classes inherit from a single superclass.​



java​
CopyEdit​
class Animal {
void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal {


void bark() { System.out.println("Dog barks"); }
}

class Cat extends Animal {


void meow() { System.out.println("Cat meows"); }
}

●​

Code Reusability: Inheritance allows subclasses to reuse the fields and methods of the
superclass, reducing redundancy and making the code more modular.​

java​
CopyEdit​
class Animal {
void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal {


void bark() { System.out.println("Dog barks"); }
}

// Dog inherits eat() method from Animal

●​

3. What is Polymorphism in Java? Explain method overriding with an


example.

Answer:​
Polymorphism is a concept where one object can take many forms. It allows methods to
have the same name but perform different tasks. Polymorphism can be achieved through
method overloading and method overriding.

●​ Method Overloading: Same method name with different parameters.​

java
CopyEdit
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}

●​ Method Overriding: A subclass provides its own implementation of a method that is


already defined in the superclass.​

java
CopyEdit
class Animal {
void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {


@Override
void sound() { System.out.println("Bark"); }
}

public class Test {


public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();

myAnimal.sound(); // Calls Animal's sound()


myDog.sound(); // Calls Dog's sound() - Overriding in
action
}
}

4. What is the difference between method overloading and method


overriding in Java?
Answer:

●​ Method Overloading: Occurs when two or more methods in the same class have
the same name but differ in the number or type of parameters.​

○​ It is determined at compile-time (compile-time polymorphism).​

○​ It is used to perform similar operations with different arguments.​

java​
CopyEdit​
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}

●​
●​ Method Overriding: Occurs when a subclass provides its own implementation of a
method that is already defined in its superclass.​

○​ It is determined at runtime (runtime polymorphism).​

○​ It is used to modify or extend the behavior of an inherited method.​

java​
CopyEdit​
class Animal {
void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {


@Override
void sound() { System.out.println("Bark"); }
}

●​

5. What is an interface in Java? How is it different from an abstract


class?

Answer:
●​ Interface: A reference type, similar to a class, that can contain only abstract methods
(methods without a body). A class implements an interface using the implements
keyword.​

○​ Interfaces cannot have method implementations (except default and static


methods).​

java​
CopyEdit​
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() { System.out.println("Bark"); }
}

●​
●​ Abstract Class: A class that can contain both abstract (unimplemented) methods
and concrete (implemented) methods. A class inherits an abstract class using the
extends keyword.​

○​ An abstract class can have constructors, instance variables, and methods


with a body.​

java​
CopyEdit​
abstract class Animal {
abstract void sound(); // Abstract method
void sleep() { System.out.println("Animal is sleeping"); }
}

●​

6. What is exception handling in Java? Explain try-catch blocks with an


example.

Answer:​
Exception Handling in Java is a mechanism to handle runtime errors, allowing the program
to continue executing. The basic blocks for exception handling are try, catch, and
finally.
●​ try: Contains code that might throw an exception.​

●​ catch: Handles the exception.​

finally: Executes code regardless of whether an exception is thrown or not.​



java​
CopyEdit​
try {
int result = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("This will always execute");
}

●​

7. What is the role of the super keyword in Java?

Answer:​
The super keyword in Java refers to the superclass of the current object. It is used to:

●​ Access the parent class's methods and variables.​

●​ Call the parent class constructor.​

java
CopyEdit
class Animal {
void eat() { System.out.println("Animal eats"); }
}

class Dog extends Animal {


void eat() {
super.eat(); // Calls parent class's eat method
System.out.println("Dog eats"); // Child class's eat method
}
}
8. What is the difference between == and .equals() in Java?

Answer:

●​ ==: Compares the reference (memory address) of two objects.​

.equals(): Compares the actual contents of two objects.​



java​
CopyEdit​
String s1 = new String("hello");
String s2 = new String("hello");

System.out.println(s1 == s2); // false (different


references)
System.out.println(s1.equals(s2)); // true (same content)

●​

9. What is the use of the final keyword in Java?

Answer:​
The final keyword is used to define constants, prevent method overriding, and prevent
inheritance:

●​ Final variable: Its value cannot be changed once assigned.​

●​ Final method: The method cannot be overridden in a subclass.​

Final class: The class cannot be subclassed.​



java​
CopyEdit​
final class Animal {
// class cannot be inherited
}

final void eat() {


// method cannot be overridden
}

●​
10. Explain the difference between ArrayList and LinkedList in Java.

Answer:

●​ ArrayList: A resizable array implementation. It allows fast random access to


elements but slow insertions and deletions.​

●​ LinkedList: A doubly-linked list implementation. It allows fast insertions and


deletions but slow random access to elements.​

○​ ArrayList: Better for applications with frequent read operations.​

○​ LinkedList: Better for applications with frequent insertions and deletions.​

11. What is a constructor in Java? Explain the types of constructors with


examples.

Answer:​
A constructor is a special method that is invoked when an object is created. It is used to
initialize the object's state.

Default Constructor: A constructor that takes no arguments. If no constructor is provided,


the compiler provides a default one.​

java​
CopyEdit​
class Person {
String name;
int age;

// Default constructor
Person() {
name = "John";
age = 25;
}
}

●​

Parameterized Constructor: A constructor that takes parameters to initialize an object with


specific values.​

java​
CopyEdit​
class Person {
String name;
int age;

// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
}

●​

12. What is an abstract class in Java? How is it different from an


interface?

Answer:​
An abstract class is a class that cannot be instantiated on its own and may contain
abstract methods (methods without a body).

●​ Difference from Interface:​

○​ An abstract class can have both abstract and concrete methods.​

○​ An interface can have only abstract methods (until Java 8 introduced default
methods).​

java​
CopyEdit​
abstract class Animal {
abstract void sound(); // Abstract method
void eat() { // Concrete method
System.out.println("Animal eats");
}
}

●​

13. What is a static method in Java? Explain with an example.


Answer:​
A static method belongs to the class rather than to any specific instance. It can be called
without creating an instance of the class.

java
CopyEdit
class MathOperations {
static int add(int a, int b) {
return a + b;
}
}

public class Test {


public static void main(String[] args) {
System.out.println(MathOperations.add(5, 3)); // Call
static method
}
}

14. Explain the difference between HashMap and TreeMap.

Answer:

●​ HashMap: Stores key-value pairs and does not maintain any order.​

○​ It provides faster lookup, insertion, and deletion operations.​

java​
CopyEdit​
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");

●​
●​ TreeMap: Stores key-value pairs in sorted order according to the natural ordering of
keys or by a comparator.​

○​ It is slower than HashMap for basic operations but maintains order.​

java​
CopyEdit​
TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "Apple");
map.put(2, "Banana");

●​

15. What is a this keyword in Java?

Answer:​
The this keyword refers to the current object. It is used to:

●​ Access instance variables.​

●​ Invoke instance methods.​

Call another constructor within the same class.​



java​
CopyEdit​
class Car {
String model;

Car(String model) {
this.model = model; // Refers to the current object's
instance variable
}
}

●​

These answers should be enough to cover the 7-mark questions for OOP in Java. Let me
know if you need further clarification!

You might also like