Last active
August 31, 2021 12:11
-
-
Save minte9/603e22a569c4006b1373fc56c882f38e to your computer and use it in GitHub Desktop.
Java / OOP / Abstract - https://www.minte9.com/java/oop-introduction-abstract-1185
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Define abstact class Animal | |
* Class Animal contains two methods, eat() and showName() | |
* One of the method is abstract | |
*/ | |
class App { | |
public static void main(String[] args) { | |
Dog d = new Dog(); | |
d.setName("MyDog"); | |
System.out.println(d.name); // MyDog | |
} | |
} | |
class Dog extends Animal { | |
@Override | |
public void eat() {} | |
} | |
/** | |
* SOLUTION | |
*/ | |
abstract class Animal { | |
public String name; | |
public abstract void eat(); | |
public void setName(String n) { | |
name = n; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment