Skip to content

Instantly share code, notes, and snippets.

@evrentan
Created September 18, 2021 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrentan/033bee53694f28c5515f8a2b7825c92f to your computer and use it in GitHub Desktop.
Save evrentan/033bee53694f28c5515f8a2b7825c92f to your computer and use it in GitHub Desktop.
Java Inheritance Example
//superclass
class Animal {
public void eat() {
System.out.println("Animal can eat");
}
}
// subclass, Dog class inherits Animal class
class Dog extends Animal {
// overrides the eat() method of the Animal superclass
@Override
public void eat() {
// call eat() method in the superclass
super.eat();
System.out.println("Dogs eat special dog food");
}
// a new method in the subclass
public void bark() {
System.out.println("Dogs bark");
}
}
class Main {
public static void main(String[] args) {
Dog newDog = new Dog();
newDog.eat();
newDog.bark();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment