Skip to content

OOPS  #206

@sowtheesh61-tech

Description

@sowtheesh61-tech

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions