So, I'm learning Java (obviously), and I just learned how to 'implement' an interface and 'extend' a class. Now I want to try and recall the information by memory without using any reference material. So if someone experienced could respond with a thumbs up that'd be great ;) And if I got it right, then we'll just leave the information for others.
Implementing an interface...
Java Code:
//This interface will hold information for cell phones
//Like saying... you can't BE a cell phone unless you have this information, at the very least
public interface CellInfo {
public void model();
public void make();
public void androidVer();
}
//Now I implement the interface for a class called Galaxy, which is a class about a specific phone
public class Galaxy implements CellInfo {
public void model() {
System.out.println("I'm a Galaxy S5.");
}
public void make() {
System.out.println("I'm made by Samsung.");
}
public void androidVer() {
System.out.println("I run Android 4.4 KitKat.");
}
}
Extending a superclass with a subclass...
Java Code:
//This superclass will hold basic information that EVERY cell phone, regardless of make or model, must have
class Phone {
int double screenSize = 5.3;
int double weight = 10.2;
int price = 300;
}
//Now I'll make a subclass that extends the Phone class, and this subclass of phones will be called iPhone
public class iPhone extends Phone {
boolean hasAppleLogo = true;
void adjustPrice() {
if (hasAppleLogo) {
//Slam on Apple...
price += 500;
}
}
}
No comments:
Post a Comment