What is method overriding?
Redefining a method in a subclass with the same signature as in its superclass.
What is a superclass?
The class whose properties are inherited by another class.
What is a subclass?
A class that inherits properties from another class (superclass).
What does the `@Override` annotation signify?
Indicates that a method is overridden from a superclass.
What is method overloading?
Defining multiple methods with the same name but different parameters in the same class.
What is method signature?
The name and parameters of a method.
What does 'inherit' mean in OOP?
To receive properties and methods from a parent class.
Define 'implementation' in the context of methods.
The code that defines what a method does.
What are Javadoc comments?
Documentation comments used to generate API documentation.
What is the purpose of a constructor?
A special method used to initialize objects of a class.
Why is method overriding useful?
Allows a subclass to provide a specific implementation of a method already defined in its superclass.
When should you override a method?
When the subclass needs to behave differently than the superclass for a particular method call.
What happens if a subclass doesn't override a method?
The subclass inherits the superclass's implementation of the method.
Why are Javadoc comments often omitted in overridden methods?
The documentation is inherited from the superclass method.
What is the relationship between inheritance and overriding?
Inheritance provides the method, overriding allows specialized behavior in subclasses.
How does overriding promote polymorphism?
Allows objects of different classes to respond to the same method call in their own way.
What is the role of the `@Override` annotation?
It signals to the compiler that the method is intended to override a superclass method, catching errors if it doesn't.
Can a subclass override a private method of its superclass?
No, private methods are not accessible to subclasses and therefore cannot be overridden.
What is the difference between overriding and overloading?
Overriding replaces a superclass method, overloading creates a new method with the same name but different parameters.
How does overriding relate to the 'is-a' relationship?
A subclass 'is-a' type of its superclass, and overriding allows it to refine the superclass's behavior.
What is the output of the following code? class Animal { public void makeSound() { System.out.println("Generic animal sound"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Dog(); myAnimal.makeSound(); } }
Woof!
Identify the error in the following code: class Parent { public void display() { System.out.println("Parent"); } } class Child extends Parent { public void show() { System.out.println("Child"); } @Override public void display(int x) { System.out.println("Child display"); } }
The `@Override` annotation is incorrect. The method `display(int x)` in the `Child` class does not override the `display()` method in the `Parent` class because they have different signatures. It's an overloaded method, not an overridden one.
What is the output of the following code? class Vehicle { public String getSpeed() { return "60 mph"; } } class Car extends Vehicle { @Override public String getSpeed() { return "100 mph"; } } public class Main { public static void main(String[] args) { Vehicle myVehicle = new Car(); System.out.println(myVehicle.getSpeed()); } }
100 mph
What is the output of the following code? class A { public int getValue() { return 5; } } class B extends A { } public class Main { public static void main(String[] args) { B b = new B(); System.out.println(b.getValue()); } }
5
What is the output of the following code? class Animal { public String speak() { return "Generic sound"; } } class Cat extends Animal { @Override public String speak() { return "Meow"; } } public class Main { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat(); System.out.println(animal.speak()); System.out.println(cat.speak()); } }
Generic sound
Meow
Identify the error in the following code: class Shape { public double area() { return 0; } } class Circle extends Shape { public double area(int radius) { return Math.PI * radius * radius; } }
The `area(int radius)` method in `Circle` is overloading, not overriding, the `area()` method in `Shape` because the signatures are different. To override, the method signature must be the same, and the `@Override` annotation is missing.
What is the output of the following code? class Parent { public void printMessage() { System.out.println("Parent message"); } } class Child extends Parent { @Override public void printMessage() { System.out.println("Child message"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.printMessage(); } }
Child message
What is the output of the following code? class A { public String method() { return "A"; } } class B extends A { @Override public String method() { return super.method() + "B"; } } public class Main { public static void main(String[] args) { B b = new B(); System.out.println(b.method()); } }
AB
Identify the error in the following code: class Animal { public void eat() { System.out.println("Animal eating"); } } class Dog extends Animal { public void Eat() { System.out.println("Dog eating"); } }
The `Eat()` method in the `Dog` class is not overriding the `eat()` method in the `Animal` class because the method names are case-sensitive and different. It should be `eat()` to override correctly.
What is the output of the following code? class Vehicle { public String accelerate() { return "Vehicle accelerating"; } } class Car extends Vehicle { @Override public String accelerate() { return "Car accelerating fast"; } } public class Main { public static void main(String[] args) { Vehicle myVehicle = new Car(); System.out.println(myVehicle.accelerate()); } }
Car accelerating fast