Skip to content

Instantly share code, notes, and snippets.

@loredonrj
Last active September 6, 2021 04:16
Show Gist options
  • Save loredonrj/ec546c37a6f8544934a96356326ec788 to your computer and use it in GitHub Desktop.
Save loredonrj/ec546c37a6f8544934a96356326ec788 to your computer and use it in GitHub Desktop.
CaveOfProgramming.com.Complete.Beginner.28.Interfaces
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
}
}
public interface Info {
public void showInfo(); // Interfaces just have the header of the method, no curly brackets
}
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");
}
}
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