ket
ket
A copy constructor is a special constructor in Java that is used to create a new object by
copying the values of an existing object of the same class.
Example:
class Person {
String name;
int age;
// Parameterized Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
// Copy Constructor
Person(Person other) {
this.name = other.name;
this.age = other.age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Output:
2. List Down Design Patterns in Java. Explain Any One with Example. (6 Marks)
1. Singleton Pattern
2. Factory Pattern
3. Observer Pattern
4. Strategy Pattern
5. Command Pattern
2
6. Adapter Pattern
7. Decorator Pattern
8. Builder Pattern
9. Prototype Pattern
10. Composite Pattern
The Factory Pattern provides a way to create objects without specifying the exact class of
object that will be created.
Example:
// Product interface
interface Product {
void create();
}
// ConcreteProduct1
class ConcreteProduct1 implements Product {
public void create() {
System.out.println("Product 1 created");
}
}
// ConcreteProduct2
class ConcreteProduct2 implements Product {
public void create() {
System.out.println("Product 2 created");
}
}
// Factory
class ProductFactory {
public Product createProduct(String productType) {
if (productType.equals("Product1")) {
return new ConcreteProduct1();
} else if (productType.equals("Product2")) {
return new ConcreteProduct2();
}
return null;
}
}
Output:
Product 1 created
Product 2 created
Composition:
• Composition is a design principle where one class contains an object of another class as
a member. It represents a "has-a" relationship between classes. If the container class is
destroyed, the contained objects are destroyed as well.
Example:
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
private Engine engine;
Car() {
engine = new Engine();
}
void drive() {
engine.start();
System.out.println("Car is driving");
}
}
Delegation:
4
• Delegation is a design pattern where one object handles a request by delegating the task
to another object. It helps in separating concerns and enhancing reusability.
Example:
interface Printer {
void print();
}
class PrintService {
private Printer printer;
PrintService(Printer printer) {
this.printer = printer;
}
void delegatePrint() {
printer.print();
}
}
Diagram:
5
A generic class is a class that can work with any data type. Java provides generics to ensure type
safety and to eliminate the need for casting.
Example:
class Box<T> {
private T value;
public T getValue() {
return value;
}
}
Output:
10
Hello
A bounded wildcard allows you to specify a range of types that can be passed to a method. You
can specify an upper bound or lower bound for the type parameter.
• Upper Bounded Wildcard (? extends T): Restricts the type parameter to be of type T
or a subclass of T.
6
• Lower Bounded Wildcard (? super T): Restricts the type parameter to be of type T or
a superclass of T.
Example:
class Fruit {}
class Apple extends Fruit {}
class Banana extends Fruit {}
printFruits(apples);
printFruits(bananas);
}
}
7. List Out the Basic Concept of OOP. Explain Any Two in Brief. (5 Marks)
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
• Encapsulation: It is the mechanism of hiding the internal details of an object and only
exposing the necessary functionalities. This is typically achieved using private variables
and public methods.
• Polymorphism: It allows one interface to be used for different data types. It can be
achieved through method overloading (compile-time polymorphism) and method
overriding (runtime polymorphism).
The this keyword is used to refer to the current instance of the class. It is used:
• To differentiate between instance variables and local variables when they have the same
name.
• To invoke the current class method or constructor.
Example:
class Car {
String model;
Car(String model) {
this.model = model; // "this" refers to the current object
}
void display() {
System.out.println("Model: " + this.model);
}
}
Inheritance is a mechanism where one class acquires the properties and behaviors of another
class. It supports the "is-a" relationship.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
dog.eat();
dog.bark();
}
}
Method Overloading is the ability to define multiple methods with the same name but with
different parameters.
Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
Method Overriding occurs when a subclass provides a specific implementation of a method that
is already defined in its superclass.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
An abstract class is a class that cannot be instantiated directly. It can have abstract methods
(without implementation) and concrete methods (with implementation).
Example:
void eat() {
System.out.println("Animal is eating");
}
}
In Java, multiple inheritance is not allowed directly with classes (i.e., a class cannot inherit
from more than one class). However, it can be achieved using interfaces.
Example:
10
interface Animal {
void eat();
}
interface Bird {
void fly();
}
14. Explain Parameterized Collection and Explain Any One with Example. (4
Marks)
A parameterized collection allows collections to hold data of a specific type, ensuring type
safety. It is typically used with generics.
Example (ArrayList):
import java.util.ArrayList;
15. Enlist Advanced Collection Types and Explain Any One with Example. (4
Marks)
1. ArrayList
2. LinkedList
3. HashMap
4. HashSet
5. TreeMap
6. TreeSet
7. PriorityQueue
Example: HashMap
import java.util.HashMap;
System.out.println(map.get("Apple"));
}
}
A generic type interface defines a contract that can be implemented by any class using a
specific data type.
Example:
interface Box<T> {
void setValue(T value);
T getValue();
}