Skip to content

Instantly share code, notes, and snippets.

Created September 18, 2021 22:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Java Inheritance Example
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
public void eat() {
// call eat() method in the superclass;
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();;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment