Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
114 views
in JAVA by (178k points)
Can a subclass override a method of the superclass in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

Yes, a subclass can override a method of the superclass. When a subclass defines a method with the same name, return type, and parameters as a method of the superclass, it is said to override the method of the superclass. The subclass's method implementation will be called instead of the superclass method implementation when the method is invoked on an object of the subclass.

Example Code:

class Animal {
   void eat() {
      System.out.println("The animal is eating");
   }
}

class Dog extends Animal {
   @Override
   void eat() {
      System.out.println("The dog is eating");
   }
}

public class Main {
   public static void main(String[] args) {
      Dog d = new Dog();
      d.eat(); // The dog is eating
   }
}
 

In the above code, the Dog class overrides the eat() method of the Animal class. When the eat() method is called on an object of the Dog class, the Dog class's implementation of the method is called, which prints "The dog is eating" to the console.

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...