The document provides a Java program demonstrating method overriding and the use of the 'super' keyword. It defines a parent class 'Animal' with a method 'sound' and a child class 'Dog' that overrides this method. The program outputs the overridden sound and messages from both the parent and child classes when executed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views
4.B JAVA
The document provides a Java program demonstrating method overriding and the use of the 'super' keyword. It defines a parent class 'Animal' with a method 'sound' and a child class 'Dog' that overrides this method. The program outputs the overridden sound and messages from both the parent and child classes when executed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
4.b Write a java program to implement overriding that shows use of super keyword.
// Parent class class Animal { // Method in the parent class void sound() { System.out.println("Animal makes a sound"); }
// Method to show usage of 'super' keyword
void showMessage() { System.out.println("Message from Animal class"); } }
// Child class that overrides the sound() method of Animal class
class Dog extends Animal { // Overriding the sound() method @Override void sound() { System.out.println("Dog barks"); }
// Method to demonstrate usage of super
void showMessage() { // Calling the method of the parent class using super super.showMessage(); System.out.println("Message from Dog class"); } } public class Main { public static void main(String[] args) { // Create an object of the Dog class Dog dog = new Dog();
// Calling the overridden sound() method
dog.sound(); // Output: Dog barks
// Calling the method that demonstrates super keyword
dog.showMessage(); // Output: // Message from Animal class // Message from Dog class } }
OUTPUT Dog barks Message from Animal class Message from Dog class