- Notifications
You must be signed in to change notification settings - Fork 1k
Open
Description
class Animal{
String name;
public void eat(){
System.out.println(name + " is eating.");
}
}
class Dog extends Animal{
String breed;
public void bark(){System.out.println(name + " the " + breed + " is barking.")} }
public class InheritanceExample{
public static void main(String[] args){
Dog myDog = new Dog(); myDog.name = "Buddy"; myDog.breed = "Golden Retriever"; myDog.eat(); myDog.bark()} }
METHOD OVEERIDING
class Animal{
public void makeSound(){
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal{
public void makeSound(){
System.out.println("Dog barks.");
}
}
public class MethodOverridingDemo{
public static void main(String[] args){
Animal genericAnimal = new Animal();
genericAnimal.makeSound(); Dog myDog = new Dog(); myDog.makeSound(); Animal polymorphicAnimal = new Dog(); polymorphicAnimal.makeSound()} }
Metadata
Metadata
Assignees
Labels
No labels