File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Animal {
2+ // Instance variable
3+ String name ;
4+
5+ // Method to make a generic sound
6+ public void makeSound () {
7+ System .out .println ("Some sound..." );
8+ }
9+
10+ // Setter for name
11+ public void setName (String name ) {
12+ this .name = name ;
13+ }
14+
15+ // Getter for name
16+ public String getName () {
17+ return name ;
18+ }
19+ }
20+
21+ class Dog extends Animal {
22+ // Overriding the makeSound method
23+ @ Override
24+ public void makeSound () {
25+ System .out .println ("Bark! Bark!" );
26+ }
27+ }
28+
29+
30+ public class Animals {
31+ public static void main (String [] args ) {
32+ // Create an instance of Dog
33+ Dog myDog = new Dog ();
34+
35+ // Set the dog's name
36+ myDog .setName ("Buddy" );
37+
38+ // Call makeSound()
39+ myDog .makeSound ();
40+
41+ // Print the dog's name
42+ System .out .println ("Dog's name is: " + myDog .getName ());
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments