Answer:
- Class: A blueprint or template for creating objects. It defines properties (fields) and behaviors (methods).
- Object: An instance of a class. It represents a real-world entity with a state (attributes) and behavior (methods).
class Car {
String brand;
int speed;
void accelerate() {
speed += 10;
}
}
Car car = new Car(); // Object of class CarAnswer:
- Method overloading allows multiple methods with the same name but different parameter lists (type, number, or order).
- It provides compile-time polymorphism.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}Answer:
- Method Overriding: Redefining a method in a subclass that exists in the parent class. It provides runtime polymorphism.
- Difference:
- Overloading happens within the same class, overriding occurs across a superclass and subclass.
- Overloading is resolved at compile time, overriding at runtime.
class Parent {
void display() {
System.out.println("Parent method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child method");
}
}Answer:
| Feature | Abstract Class | Interface |
|---|---|---|
| Inheritance | Single inheritance | Multiple inheritance |
| Method Types | Can have abstract and concrete | Only abstract methods (until Java 7), default and static methods (Java 8+) |
| Fields | Can have instance variables | Only constants |
| Keywords | abstract |
interface |
abstract class Animal {
abstract void makeSound();
}
interface Vehicle {
void start();
}Answer:
- Multiple inheritance refers to a class inheriting from more than one class or interface.
- Java does not support multiple inheritance with classes to avoid the Diamond Problem.
- It is achieved using interfaces.
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}Answer:
- Role: Constructors initialize an object when it is created.
- Yes, we can have a private constructor. It is used in Singleton design patterns.
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}Answer:
superis used to access methods or constructors of the parent class.- It is used to:
- Call the superclass constructor.
- Access superclass methods and fields.
class Parent {
Parent(String name) {
System.out.println("Parent: " + name);
}
}
class Child extends Parent {
Child(String name) {
super(name);
}
}Answer:
- Static Binding: Method calls resolved at compile-time (e.g., private, final, or static methods).
- Dynamic Binding: Method calls resolved at runtime (e.g., overridden methods).
class Parent {
void display() {
System.out.println("Parent method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child method");
}
}Answer:
- Inner Class: A class defined within another class.
- Static Inner Class:
- Can be accessed without creating an instance of the outer class.
- Does not have access to instance members of the outer class.
- Non-Static Inner Class:
- Requires an instance of the outer class.
- Can access all members of the outer class.
class Outer {
class NonStaticInner {}
static class StaticInner {}
}Answer:
- Objects are compared using:
==for reference comparison..equals()for value comparison.
compareTo():- Used for natural ordering (e.g., sorting).
- Returns:
- Negative: Current object < argument.
- Zero: Current object == argument.
- Positive: Current object > argument.
class Person implements Comparable<Person> {
String name;
public int compareTo(Person other) {
return this.name.compareTo(other.name);
}
}