Skip to content
Permalink
Browse files
minor
  • Loading branch information
Jianhua Yang committed Oct 23, 2017
1 parent 1487899 commit 2bbf5bd43ad4e371d80e55ee4a3c2166a7e19896
Showing 1 changed file with 7 additions and 7 deletions.
@@ -515,7 +515,7 @@ Go back to Vehicle.java, we need to prepare the class to make it ready.
```
An interface is like a class, it defines a set of *empty* methods. Any non-abstract classes that implement the interface must provide a method body for each of these methods. In this sense, we often call an interface as a 'contract' - it defines what the class is capable of i.e. its methods.

> Interface in Java is so important that there's a principle called ['programming to an interface'](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface).
> Interface in Java is so important that there's a principle called ['programming to an interface'](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface).
Note the method defined in the interface has no curly brackets and is terminated with a semicolon. This is different from the ordinary method.

@@ -584,12 +584,12 @@ Now cenes are set up, we're ready to explore inheritance in java:

What this code does is that we create two subclasses of Vehicle, each has slightly different structure i.e.viariables/methods. Car has an additional field called color, and Diesel has one called type. In addition, Diesel has an additional control() method by implementing the Controllable interface. The idea is that by looking at the class signature i.e. `class Diesel extends Vehicle implements Vehicle.Controllable` we know that the Diesel class is capable of doing methods defined in `Controllable`. Also note that:

* 'extends' defines a relationship between superclass (Vehicle) and subclass (Car). A subclass inherits all non-private instance variables and methods. That is why we didn't declare anything such as make/year, but we can still use them. An interesting question often asked by beginners in Java is ['Do subclasses inherit private fields?'](http://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields).
* In subclasses we can define additional instance variables or methods such as color for Car.
* 'implements' defines a relationship between a class (Diesel) and an interface (Vehicle.Controllable). The interface method the class implements must be public. In our case, we must have `public void control()` and it cannot be `void control()`
* Note the super keyword in the control() method, this is similar to 'this' keyword -- i.e. it refers to the superclass in the hierarchy. If we don't user the super keyword in our case `super.getMessage()`, we'll end up with infinite loops as the default behavior is to call `this.getMessage()`.
> If you remove 'super' in control() method and run your app, what happens?
* 'extends' defines a relationship between superclass (Vehicle) and subclass (Car). A subclass inherits all non-private instance variables andmethods. That is why we didn't declare anything such as make/year, but we can still use them. An interesting question often asked by beginners inJava is ['Do subclasses inherit private fields?'](http://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields).
* In subclasses we can define additional instance variables or methods such as color for Car.
* 'implements' defines a relationship between a class (Diesel) and an interface (Vehicle.Controllable). The interface method the class implementsmust be public. In our case, we must have `public void control()` and it cannot be `void control()`
* Note the super keyword in the control() method, this is similar to 'this' keyword -- i.e. it refers to the superclass in the hierarchy. If wedon't user the super keyword in our case `super.getMessage()`, we'll end up with infinite loops as the default behavior is to call `this.getMessage(`.

> If you remove 'super' in control() method and run your app, what happens?
4. Next, you need to link the layout with classes. Go back to MainActivity.java and insert variable declarations

0 comments on commit 2bbf5bd

Please sign in to comment.