Overriding
Description
A child can replace inherited behavior.
Explanation
@Override asks the compiler to verify the method signature.
Example
@Override
void study() { System.out.println("Research"); }← Chapter 6 — Object-Oriented Programming
mixed
Polymorphism lets code work through a shared type while objects provide their own implementation. It reduces duplicate code when several classes can perform the same action.
Abstract classes can share partial implementation. Interfaces define a contract that unrelated classes can implement.
A child can replace inherited behavior.
@Override asks the compiler to verify the method signature.
@Override
void study() { System.out.println("Research"); }An interface defines behavior a class promises to provide.
A class can implement more than one interface.
interface Printable { void print(); }Declare variables with the most useful shared type, such as Printable item. This allows different implementations to be substituted without changing the calling code.
public class PolymorphismDemo {
interface Greeter { void greet(); }
static class Student implements Greeter {
public void greet() { System.out.println("Hello from Aditi"); }
}
public static void main(String[] args) {
Greeter learner = new Student();
learner.greet();
}
}