Oops With Jaava
Oops With Jaava
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;
●
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
}
●
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"); }
}
●
● Polymorphism:
Polymorphism allows objects to take many forms. It is achieved by overriding or
overloading methods.
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"); }
}
●
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.
●
●
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"); }
}
●
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.
java
CopyEdit
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
java
CopyEdit
class Animal {
void sound() { System.out.println("Animal sound"); }
}
● 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.
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.
java
CopyEdit
class Animal {
void sound() { System.out.println("Animal sound"); }
}
●
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.
java
CopyEdit
interface Animal {
void sound();
}
●
● 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.
java
CopyEdit
abstract class Animal {
abstract void sound(); // Abstract method
void sleep() { System.out.println("Animal is sleeping"); }
}
●
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.
●
Answer:
The super keyword in Java refers to the superclass of the current object. It is used to:
java
CopyEdit
class Animal {
void eat() { System.out.println("Animal eats"); }
}
Answer:
●
Answer:
The final keyword is used to define constants, prevent method overriding, and prevent
inheritance:
●
10. Explain the difference between ArrayList and LinkedList in Java.
Answer:
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
Person() {
name = "John";
age = 25;
}
}
●
// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
●
Answer:
An abstract class is a class that cannot be instantiated on its own and may contain
abstract methods (methods without a body).
○ 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");
}
}
●
java
CopyEdit
class MathOperations {
static int add(int a, int b) {
return a + b;
}
}
Answer:
● HashMap: Stores key-value pairs and does not maintain any order.
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.
java
CopyEdit
TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
●
Answer:
The this keyword refers to the current object. It is used to:
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!