Java Interview Questions
Java Interview Questions
// Superclass method
void display() {
System.out.println("I am an animal. My name is " + name);
}
}
// Subclass
class Dog extends Animal {
String breed;
// Subclass constructor
Dog(String name, String breed) {
super(name); // Call superclass constructor
this.breed = breed;
}
// Subclass method
void display() {
super.display(); // Call superclass method
System.out.println("I am a dog. My breed is " + breed);
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create an object of the subclass
Dog dog = new Dog("Buddy", "Golden Retriever");
class Counter {
static int count = 0; // static variable
Counter() {
count++;
}
void displayCount() {
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
Counter obj1 = new Counter();
Counter obj2 = new Counter();
Counter obj3 = new Counter();
obj1.displayCount(); // Count: 3
obj2.displayCount(); // Count: 3
obj3.displayCount(); // Count: 3
}
}
2. Static Methods
Static methods belong to the class rather than any specific instance. They can be called without
creating an instance of the class. Static methods can only access static variables and other static
methods directly.
Example-
class MathUtils {
// static method
static int add(int a, int b) {
return a + b;
}
}
3. Static Blocks
Static blocks are used to initialize static variables. They are executed when the class is loaded.
Example –
class StaticBlockExample {
static int value;
// static block
static {
value = 42;
System.out.println("Static block initialized.");
}
}
1. Final Variables
A final variable can be assigned only once. Once a final variable has been assigned, it
always contains the same value.
Example -
public class Main {
public static void main(String[] args) {
final int constantValue = 10;
System.out.println("Constant Value: " + constantValue);
2. Final Methods
A final method cannot be overridden by subclasses. This is useful when you want to prevent
subclasses from changing the implementation of a method.
Example -
class BaseClass {
final void display() {
System.out.println("This is a final method in the base class.");
}
}
final: Keyword used to declare constants, prevent method overriding, and inheritance.
A modifier that makes variable, methods or class immutable or unchangeable.
finally: Block used in exception handling to execute code after try-catch blocks, regardless of
an exception.
public class FinallyExample {
public static void main(String[] args) {
try {
int data = 25 / 0; // This will throw an exception
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e);
} finally {
System.out.println("This block always executes.");
}
}
}
finalize (): Method called by the garbage collector before an object is destroyed.
public class FinalizeExample {
// Overriding finalize method
protected void finalize() throws Throwable {
System.out.println("Finalize method called.");
}
// Interface 2
interface Bird {
void fly();
}