Last active
September 6, 2021 04:16
-
-
Save loredonrj/ec546c37a6f8544934a96356326ec788 to your computer and use it in GitHub Desktop.
CaveOfProgramming.com.Complete.Beginner.28.Interfaces
This file contains hidden or 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
public class App{ | |
//test | |
public static void main(String[] args) { | |
Machine machine1 = new Machine(); | |
machine1.start(); | |
Person person1 = new Person("Lily"); | |
person1.greet(); | |
Info info1 = new Machine(); // Illustrate possible usage of Info Interface as a Type, like a Class | |
info1.showInfo(); // the created object of Type Machine is assigned to a variable of type Info, and the method in Info Interface can be called on it | |
// which is really cool | |
} | |
} |
This file contains hidden or 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
public interface Info { | |
public void showInfo(); // Interfaces just have the header of the method, no curly brackets | |
} |
This file contains hidden or 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
public class Machine implements Info{ | |
@Override | |
public void showInfo() { | |
System.out.println("Machine ID is" + id); | |
} | |
private int id = 7; | |
public void start(){ | |
System.out.println("Machine Started"); | |
} | |
} |
This file contains hidden or 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
public class Person implements Info { | |
//change 23:57 | |
private String name; | |
public Person(String name) { | |
this.name = name; | |
} | |
public void greet(){ | |
System.out.println("Greetings!"); | |
} | |
@Override | |
public void showInfo() { | |
System.out.println("Person name is" + name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment