Review Class Inheritance Abstract Interfaces Polymorphism GUI MVC
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Inheritance allows a class to use the properties and methods of another class while adding its own functionality for example: you could create a generic student class with states and actions that are common to all students parent, aka, superclass, base class then, more specific classes could be created for part-time, fulltime, and continuing students children, aka, subclasses, derived classes enhances the ability to reuse code makes design a much simpler and cleaner 2 This slide is based on: References
Inheritance the Object class is the highest superclass of Java all other classes are subclasses inheriting from it we use the extends keyword to set the relationship between a superclass and a subclass you can override methods, that is to create a new set of method statements for the same method signature 3 method signature includes the name, the number of parameters, and the parameter types you cannot override final methods, methods in final classes, and private or static methods This slide is based on: References
Inheritance when extending a class constructor you can reuse the superclass constructor and overridden superclass methods by using the reserved word super this reference must come first in the subclass constructor the reserved word this is used to distinguish between the object's property and the passed in parameter could be used to reference private constructors as well 4 beneficial for initializing properties This slide is based on: References
Inheritance – Superclass public class Animal { public void sleep() { System. out. println("Sleeping"); } public void walk() { System. out. println("Walking"); } public void eat() { System. out. println("Eating"); } } 5 This slide is based on: References
Inheritance – Subclasses public class Dog extends Animal { public void bark() { System. out. println("Woof!"); } } public class Cat extends Animal { public void meow() { System. out. println("Meow!"); } } Dog my. Dog = new Dog(); my. Dog. eat(); 6 This slide is based on: References
Abstract a superclass is more general than its subclasses and contains elements and properties common to all of the subclasses a superclass could be set up as an abstract class does not allow objects of its prototype to be created only objects of the subclass are used forcing the client to create specific animals like a Cat or a Dog 7 This slide is based on: References
Abstract – Superclass public abstract class Animal { public void sleep() { System. out. println("Sleeping"); } public void walk() { System. out. println("Walking"); } public void eat() { System. out. println("Eating"); } } Animal my. Animal = new my. Animal(); 8 X This slide is based on: References
Abstract Methods abstract methods are methods with no body specification you create a method but do not fill in the code inside of it subclasses must provide the method statements for their particular meaning 9 would require overriding in each subclass, the applied method statements may be inappropriate otherwise This slide is based on: References
Abstract Method in Superclass public abstract class Animal { public void sleep() { System. out. println("Sleeping"); } public void walk() { System. out. println("Walking"); } public void eat() { System. out. println("Eating"); } public abstract void speak(); } 10 This slide is based on: References
Abstract Methods – Subclasses public class Dog extends Animal { public void speak() { System. out. println("Woof!"); } } public class Cat extends Animal { public void speak() { System. out. println("Meow!"); } } 11 This slide is based on: References
Interfaces similar to abstract classes but all methods are abstract and all properties are static final interfaces can be inherited the extends keyword is used for inheritance you can have a sub-interface You cannot have multiple inheritance for classes, hence, an interface is used to tie elements of several classes together used to separate design from coding as class method headers are specified but not their bodies 12 allows compilation and parameter consistency testing prior to the coding phase used to set up unit testing frameworks This slide is based on: References
Interfaces – Superclass and Dog public interface Talking { public void work(); } subclass Dog public class Dog extends Animal implements {. . . public void work() { speak(); System. out. println("Be aware of me!!"); } } 13 This slide is based on: References
Polymorphism allows an action or method to do different things based on the object that it is acting upon three types of polymorphism 1. 2. 3. 14 overloading overriding late (or dynamic) method binding This slide is based on: References
Overloaded and Overridden Methods overloaded methods with the same name signature but either a different number of parameters or different types in the parameter list overridden methods are redefined within an inherited or a subclass and have the same signature and the subclass definition is used 15 This slide is based on: References
Late Method Binding it allows a program to resolve references to subclass methods at runtime for instance, let’s assume 16 we have two subclasses Dog and Cat that are created based on the Animal abstract class they both have their own speak() method in that case, although each method reference is to an Animal, the code will resolve the correct method reference at runtime This slide is based on: References
Late Method Binding client code public class Animal. Reference { public static void main(String args[]) { Animal my. Dog = new Dog("Scooby Doo"); Cat my. Cat = new Cat("Garfield"); // now reference each as an Animal my. Dog. speak(); my. Cat. speak(); } } output for my. Dog. speak() Woof! Be aware of me!! 17 This slide is based on: References
Model-View-Controller (MVC) the Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes [Burbeck 92] 18 This slide is based on: References
Model-View-Controller (MVC) model – manages the behaviour and data of the application domain responds to requests for information about its state responds to instructions to change state usually from the view, and usually from the controller view – manages the display of information controller – interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate in Web applications view is the browser and controller is the server-side components handling the HTTP request 19 This slide is based on: References
Model-View-Controller (MVC) both the view and the controller depend on the model depends on neither 20 this separation allows the model to be built and tested independent of the visual presentation however, view and controller is sometimes implemented as one object in UI This slide is based on: References
Model-View-Controller (MVC) once the MVC objects are instantiated: the view registers as a listener on the model the controller is bound to the view any changes to the underlying data of the model immediately cause a broadcast change notification that the view receives any user actions that are performed on the view will invoke a registered listener method in the controller class the controller is given a reference to the underlying model 21 This slide is based on: References
Model-View-Controller (MVC) when a user interacts with the view: the view recognizes that a GUI action, i. e. dragging a scroll bar, etc. has occurred, using a listener method that is registered to be called when such an action occurs the view calls the appropriate method on the controller accesses the model, possibly updating it in a way appropriate to the user's action. 22 if the model has been altered, it notifies interested listeners, such as the view, of the change This slide is based on: References
Model-View-Controller (MVC) 23 This slide is based on: References
Model-View-Controller (MVC) 24 This slide is based on: References
References inheritance, abstract, interfaces, and polymorphism John W. M. Russell’s Notes Java Made Easy The Java. TM Tutorial MVC 25 The Java. TM Tutorial Web Presentation Patterns Java SE Application Design With MVC
Writtentest similar to writtentest #1 and writtentest #2 10 true/false 10 short questions explanations/procedures methods, etc. = 10 marks = 10 -20 marks = 30 -40 marks total = 60 -80 marks 26
Labtest will be combination of several topics that we have discussed: GUI X recursion linked lists/arrays X GUI etc. practice: 27 PExs exercises from the recommended book examples from the lecture notes and the slides
- Slides: 27