Method Overriding in Inheritance
// Parent class
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
// Child class
class Dog extends Animal {
// Overriding the sound() method
@Override
void sound() {
[Link]("Dog barks");
}
}
// Another child class
class Cat extends Animal {
@Override
void sound() {
[Link]("Cat meows");
}
}
// Main class
public class MethodOverridingDemo {
public static void main(String[] args) {
//Animal a; // Reference of parent class
Dog a = new Dog(); // Upcasting
[Link](); // Calls Dog's sound()
a = new Cat();
[Link](); // Calls Cat's sound()
}
}
Single inheritance with super keyword
// Parent class
class Animal {
String name;
// Parent class constructor
Animal(String name) {
[Link] = name;
[Link]("Animal constructor called");
}
void display() {
[Link]("I am an animal. My name is " + name);
}
}
// Child class (Single Inheritance)
class Dog extends Animal {
String breed;
// Child class constructor
Dog(String name, String breed) {
super(name); // calling parent constructor
[Link] = breed;
[Link]("Dog constructor called");
}
void display() {
[Link](); // calling parent method
[Link]("I am a dog of breed: " + breed);
}
}
// Main class
public class SingleInheritanceSuperDemo {
public static void main(String[] args) {
Dog d = new Dog("Tommy", "Labrador");
[Link]();
}
}
Multilevel Inheritance with super
// Grandparent class
class Animal {
String name;
Animal(String name) {
[Link] = name;
[Link]("Animal constructor called");
}
void display() {
[Link]("I am an animal. My name is " + name);
}
}
// Parent class
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Calls Animal constructor
[Link] = breed;
[Link]("Dog constructor called");
}
void display() {
[Link](); // Calls Animal's display()
[Link]("I am a dog of breed: " + breed);
}
}
// Child class
class Puppy extends Dog {
int age;
Puppy(String name, String breed, int age) {
super(name, breed); // Calls Dog constructor
[Link] = age;
[Link]("Puppy constructor called");
}
void display() {
[Link](); // Calls Dog's display()
[Link]("I am a puppy, age: " + age + " months");
}
}
// Main class
public class MultilevelInheritanceSuperDemo {
public static void main(String[] args) {
Puppy p = new Puppy("Tommy", "Labrador", 6);
[Link]();
}
}
hierarchical Inheritance with super
// Parent class
class Animal {
String name;
Animal(String name) {
[Link] = name;
[Link]("Animal constructor called");
}
void display() {
[Link]("I am an animal. My name is " + name);
}
}
// Child 1
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Calls Animal constructor
[Link] = breed;
[Link]("Dog constructor called");
}
void display() {
[Link](); // Calls Animal's display()
[Link]("I am a dog of breed: " + breed);
}
}
// Child 2
class Cat extends Animal {
String color;
Cat(String name, String color) {
super(name); // Calls Animal constructor
[Link] = color;
[Link]("Cat constructor called");
}
void display() {
[Link](); // Calls Animal's display()
[Link]("I am a cat with color: " + color);
}
}
// Main class
public class HierarchicalInheritanceSuperDemo {
public static void main(String[] args) {
Dog d = new Dog("Tommy", "Labrador");
[Link]();
[Link]("------------------");
Cat c = new Cat("Kitty", "White");
[Link]();
}
}
Abstract Class
abstract class Compartment {
// Abstract method
public abstract String notice();
}
// FirstClass compartment
class FirstClass extends Compartment {
@Override
public String notice() {
return "This is First Class compartment. Reservation required.";
}
}
// Ladies compartment
class Ladies extends Compartment {
@Override
public String notice() {
return "This is Ladies compartment. Reserved for women passengers.";
}
}
// General compartment
class General extends Compartment {
@Override
public String notice() {
return "This is General compartment. Open for all passengers.";
}
}
// Luggage compartment
class Luggage extends Compartment {
@Override
public String notice() {
return "This is Luggage compartment. For carrying goods and baggage.";
}
}
// Test program
public class TestCompartment {
public static void main(String[] args) {
// Create array of compartments
Compartment[] compartments = new Compartment[4];
compartments[0] = new FirstClass();
compartments[1] = new Ladies();
compartments[2] = new General();
compartments[3] = new Luggage();
// Print notice of each compartment
for (Compartment c : compartments) {
[Link]([Link]());
}
}
}
Normal,Static,Final Variable
class VariableDemo {
int normalVar; // Normal instance variable
static int staticVar; // Static class variable
final int finalVar; // Final instance variable
// Constructor to initialize normalVar and finalVar
VariableDemo(int normalVal, int finalVal) {
normalVar = normalVal;
finalVar = finalVal; // Must initialize final variable once here
}
void display() {
[Link]("Normal Var: " + normalVar);
[Link]("Static Var: " + staticVar);
[Link]("Final Var: " + finalVar);
}
}
public class Main {
public static void main(String[] args) {
[Link] = 10; // Set static variable once (shared)
VariableDemo obj1 = new VariableDemo(1, 100);
VariableDemo obj2 = new VariableDemo(2, 200);
[Link]();
[Link]("---");
[Link]();
// Change normalVar in obj1, does not affect obj2
[Link] = 11;
// Change staticVar using obj2, affects all instances
[Link] = 20;
[Link]("--- After changes ---");
[Link]();
[Link]("---");
[Link]();
// Uncommenting below line will cause a compilation error
// [Link] = 500; // Cannot assign a value to final va
}
Final Keyword:
1. What does the final keyword signify when applied to a variable in Java?
A) The variable can be changed any number of times.
B) The variable cannot be assigned a value more than once.
C) The variable becomes static.
D) The variable can only hold integer values.
2. Which of the following statements about final methods is TRUE?
A) A final method can be overridden by subclasses.
B) A final method cannot be overridden by subclasses.
C) A final method can only be called within the class it is defined.
D) A final method must be abstract.
3. What happens if you declare a class as final in Java?
A) The class can be subclassed.
B) The class cannot be subclassed.
C) The class cannot be instantiated.
D) The class can only contain static methods.
4. Which of the following is a valid way to declare a final variable?
B) final int x = 10;
C) int final x = 10;
D) int x final = 10;
5. Can a final variable be initialized inside a constructor?
A) Yes, but only once per object creation.
B) No, it must be initialized where it is declared.
C) Yes, and it can be changed multiple times.
D) No, final variables cannot be initialized at all.
6. What is true about a final reference variable?
A) The object it points to can be modified, but the reference cannot point to another object.
B) Neither the reference nor the object can be changed.
C) Both the reference and the object can be changed.
D) The reference must always point to null.
7. What happens if you try to override a final method in a subclass?
A) The code compiles without errors.
B) The code throws a runtime exception.
C) The code fails to compile with an error.
D) The method gets automatically removed in subclass.
8. Can a final variable be static?
A) No, final variables cannot be static.
B) Yes, and it usually represents a constant value.
C) Only if declared inside a method.
D) Only if initialized to zero.
INTERFACE EXAMPLES:
// Define an interface
interface Animal {
void sound(); // abstract method
}
// Implementing class
class Dog implements Animal {
🐶");
public void sound() {
[Link]("Dog barks
}
}
class Cat implements Animal {
🐱");
public void sound() {
[Link]("Cat meows
}
}
public class InterfaceExample1 {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
[Link](); // Dog barks
[Link](); // Cat meows
}}
Interface with Multiple Implementations
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
🔑
public void start() {
[Link]("Car starts with key ");
}
🛑
public void stop() {
[Link]("Car stops with brake ");
}
}
class Bike implements Vehicle {
🔘
public void start() {
[Link]("Bike starts with button ");
}
🏍️");
public void stop() {
[Link]("Bike stops with clutch + brake
}
}
public class InterfaceExample2 {
public static void main(String[] args) {
Vehicle v1 = new Car();
Vehicle v2 = new Bike();
[Link]();
[Link]();
[Link]();
[Link](); }}
Interface Inheritance
interface A {
void methodA();
}
interface B extends A { // Interface B inherits A
void methodB();
}
class MyClass implements B {
public void methodA() {
[Link]("Method A implemented");
}
public void methodB() {
[Link]("Method B implemented");
}
}
public class InterfaceExample3 {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
[Link](); }}
Interface with Constants
interface Maths {
int PI = 3; // public static final by default
int add(int a, int b);
}
class Calculator implements Maths {
public int add(int a, int b) {
return a + b + PI; // can use PI constant
}
}
public class InterfaceExample4 {
public static void main(String[] args) {
Calculator c = new Calculator();
[Link]("Result = " + [Link](5, 7));
}}
Multiple Interface Implementation
// First interface
interface Printer {
void print();
}
// Second interface
interface Scanner {
void scan();
}
// Class implementing both interfaces
class MultiFunctionMachine implements Printer, Scanner {
public void print() {
[Link]("Printing document...");
}
public void scan() {
[Link]("Scanning document...");
}
}
public class MultipleInterfaceExample {
public static void main(String[] args) {
MultiFunctionMachine mfm = new MultiFunctionMachine();
// Using Printer reference
Printer p = mfm;
[Link]();
// Using Scanner reference
Scanner s = mfm;
[Link]();
// Directly using object
[Link]();
[Link]();
}}
Example 2:
// Interface 1: Camera
interface Camera {
void takePhoto();
void recordVideo();
}
// Interface 2: Music Player
interface MusicPlayer {
void playMusic(String song);
void stopMusic();
}
// Interface 3: GPS
interface GPS {
void navigate(String location);
}
// Smartphone implements all interfaces
class Smartphone implements Camera, MusicPlayer, GPS {
📸
public void takePhoto() {
[Link](" Taking a photo...");
}
🎥
public void recordVideo() {
[Link](" Recording video...");
}
🎶
public void playMusic(String song) {
[Link](" Playing song: " + song);
}
⏹️
public void stopMusic() {
[Link](" Stopping music...");
}
🗺️
public void navigate(String location) {
[Link](" Navigating to: " + location);
}
}
// Main class
public class RealWorldInterfaceExample {
public static void main(String[] args) {
Smartphone phone = new Smartphone();
// Using Camera features
[Link]();
[Link]();
// Using MusicPlayer features
[Link]("Shape of You");
[Link]();
// Using GPS features
[Link]("Chennai Central Railway Station");
}
}
TT-OOP
3. Search an element in a rotated sorted array
[4,5,6,7,0,1,2]
Mid=7
Check with mid ele
If left<mid
Tar>=left && tar<mid
Check target with left[0],[1],[2,]
Fails
Check with right array
Tar>mid+1 && tar<=right
4,567012
Check taget with mid[1+2+3