Inheritance is a fundamental concept in object-oriented programming where a new class is created from an existing class. In this Java code, we have an Animal class with a method 'walk'. A new class, Bird, is created which extends the Animal class, inheriting its methods and adding new ones like 'fly' and 'sing'.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution creates a Bird object and calls the methods 'walk', 'fly', and 'sing'. The 'walk' method is inherited from the Animal class, while 'fly' and 'sing' are methods of the Bird class. This demonstrates how a subclass can inherit methods from a superclass and also define its own methods.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.oop;
/**
*
*/
class Animal{
void walk(){
System.out.println("I am walking");
}
}
class Bird extends Animal{
void fly(){
System.out.println("I am flying");
}
//code need to be added
void sing(){
System.out.println("I am singing");
}
}
public class JavaInheritanceI {
public static void main(String args[]){
Bird bird = new Bird();
bird.walk();
bird.fly();
bird.sing();
}
}
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.