📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
- The method does override or implement a method declared in a supertype.
- The method has a signature that is override-equivalent to that of any public method declared in Object.
Example 1: Simple Example
package net.javaguides.annotations; public class OverrideAnnotationExample { public static void main(String[] args) { BaseClass baseClass = new SubClass(); baseClass.test(); SubClass subClass = new SubClass(); subClass.test(); } } class BaseClass { public void test() { System.out.println("Inside baseclass"); } } class SubClass extends BaseClass { @Override public void test() { System.out.println("Inside subclass"); } }
Inside subclass
Inside subclass
Example 2: Using Abstract Class
package net.javaguides.annotations;
public class OverrideAnnotationExample {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
Animal cat = new Cat();
cat.sound();
}
}
abstract class Animal {
public abstract void sound();
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bou bou");
}
}
class Cat extends Animal {
@Override
public void sound() {
System.out.println("Meow");
}
}
Bou bou
Meow
Example 3: Using Interface
package net.javaguides.annotations;
public interface StudentService {
public void save(Student student);
public void update(Student student);
public void delete(int id);
public Student get(int id);
}
class Student {
private int id;
private String name;
}
public class StudentServiceImpl implements StudentService {
@Override
public void save(Student student) {
// TODO Auto-generated method stub
}
@Override
public void update(Student student) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
@Override
public Student get(int id) {
// TODO Auto-generated method stub
return null;
}
}
Comments
Post a Comment
Leave Comment