1 / 19

So You Think You Know Inheritance…

So You Think You Know Inheritance…. Courtesy of Saqib Ashraf. You guys have already learned abstract, sub and super classes. Now lets take it up one more level!. Interfaces!. Courtesy of Saqib Ashraf. What is an interface?.

Télécharger la présentation

So You Think You Know Inheritance…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. So You Think You Know Inheritance… Courtesy of Saqib Ashraf

  2. You guys have already learned abstract, sub and super classes Now lets take it up one more level! Interfaces! Springbrook High School Courtesy of Saqib Ashraf

  3. What is an interface? -An interface is a type of an abstract class that contains abstract methods. The methods in each interface provide a framework of behavior for any class. -Interfaces are classes that may be adapted by more than one type of class. -Interfaces usually represent something that “able” can be added to Springbrook High School Courtesy of Saqib Ashraf

  4. [little side note, this interface has “able” at the end] proSkaterObject Grindable interface Rail Skateboard Curb For Example Each pro skater object which is grindable, implements the Grindable interface. Springbrook High School Courtesy of Saqib Ashraf

  5. public interface Grindable{ void showDamage(); Boolean isGrinded(); } How do we define interfaces? Declare it an “interface” class. Even though the class is abstract, for the class to be an interface, it needs to be declared as one! Create the methods and keep them un-instantiated. Since the class is an interface, the methods are understood to be abstract and do not need to be declared so. IMPORTANT Interfaces do NOT contain instance variables!!!! Springbrook High School Courtesy of Saqib Ashraf

  6. public class Rail extends proSkaterObject implements Grindable { //variables and methods } How do we use interfaces? The extends clause must precede the implements clause We use an interface by using the key word implements The class has its own methods and variables, as well as the methods from the interface that are overridden Springbrook High School Courtesy of Saqib Ashraf

  7. interface Abstract Class Subclass of abstract class Subclass of abstract class An interface hierarchy usually looks like this: Springbrook High School Courtesy of Saqib Ashraf

  8. Let’s apply interfaces in the real world Springbrook High School Courtesy of Saqib Ashraf

  9. Tony Hawk’s Pro Skater games are a great example of how interfaces are used noob score! Sick Grind Remember that pro skater hierarchy, lets revisit that… Springbrook High School Courtesy of Saqib Ashraf

  10. pSObject Grindable interface Rail Skateboard Curb Refresher This looks really confusing, but its not. Let’s break it down to see where the interface is used Springbrook High School Courtesy of Saqib Ashraf

  11. Grindable interface Rail Curb Interface Hierarchy This is just your ordinary inheritance hierarchy. Now, lets see how each class specifically uses the interface Springbrook High School Courtesy of Saqib Ashraf

  12. A Closer Look[Gotta love those UML Diagrams] interface Grindable No instance variables allowed! boolean isGrinded(); void showDamage(); Curb extends pSObject implements Grindable Rail extends pSObject implements Grindable //instance variables //instance variables boolean isGrinded();//check if object is grinded void showDamage();//chipped pieces boolean isGrinded();//check if object is grinded void showDamage();//show sparks Each subclass overrides the methods of the interface Interface is referenced by using the implements keyword Springbrook High School Courtesy of Saqib Ashraf

  13. interface Grindable No instance variables allowed! boolean isGrinded(); void showDamage(); Curb extends pSObject implements Grindable Rail extends pSObject implements Grindable //instance variables //instance variables //instance variables boolean isGrinded();//check if object is grinded void showDamage();//chipped pieces boolean isGrinded();//check if object is grinded void showDamage();//show sparks A Closer Look[Gotta love those UML Diagrams] Important: Interfaces do NOT have instance variables Important: The extends clause is ALWAYS before the implements clause Springbrook High School Courtesy of Saqib Ashraf

  14. A couple of things to remember about interfaces -Interfaces are abstract, and their methods MUST remain un-instantiated -Classes can implement as many interfaces as they want -Interfaces are abstract, and subclasses that don’t override their methods become abstract too! -The extends clause must ALWAYS come before the implements clause Springbrook High School Courtesy of Saqib Ashraf

  15. public interface Comparable { int compareTo(Object obj); } The Comparable Interface The standard java.lang package includes the Comparable interface, which contains a very useful method for comparing objects Any class that implements Comparable must override the compareTo method! Springbrook High School Courtesy of Saqib Ashraf

  16. public interface Comparable { int compareTo(Object obj); } Analysis of the compareTo method -This method compares an implicit object[this] with the parameter object “obj”. -This method will return a negative integer, zero, or a positive integer depending on whether the implicit object is less than, equal, or larger than the “obj” Quick side note: If the two objects being compared are not type compatible, then a ClassCastException is thrown. Springbrook High School Courtesy of Saqib Ashraf

  17. Springbrook High School Courtesy of Saqib Ashraf

  18. Springbrook High School Courtesy of Saqib Ashraf

  19. Springbrook High School Courtesy of Saqib Ashraf

More Related