270 likes | 278 Views
Review Class. Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC). Inheritance. a llows a class to use the properties and methods of another class while adding its own functionality for example:
E N D
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 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 • 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 staticmethods 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 privateconstructors as well • 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"); } } 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 myDog = new Dog(); myDog.eat(); 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 abstractclass • 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 Cator a Dog 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 myAnimal = new myAnimal(); 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 • 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(); } 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!"); } } 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 • you can have a sub-interface • the extends keyword is used for inheritance • 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 • 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!!"); } } 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 • overloading • overriding • late (or dynamic) method binding This slide is based on: References
Overloaded and Overridden Methods • overloaded methodswith 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 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 • we have two subclasses Dogand Cat that are created based on the Animalabstractclass • 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 AnimalReference { public static void main(String args[]) { Animal myDog= new Dog("Scooby Doo"); Cat myCat = new Cat("Garfield"); // now reference each as an Animal myDog.speak(); myCat.speak(); } } output for myDog.speak() Woof! Be aware of me!! 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 [Burbeck92] This slide is based on: References
Model-View-Controller (MVC) • model – manages the behaviourand data of the application domain • responds to requests for information about its state • usually from the view, and • responds to instructions to change state • 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 This slide is based on: References
Model-View-Controller (MVC) • both the view and the controller depend on the model • the model depends on neither • 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 • any changes to the underlying data of the model immediately cause a broadcast change notification that the view receives • the controller is bound to the view • 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 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 • the controller accesses the model, possibly updating it in a way appropriate to the user's action. • 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) This slide is based on: References
Model-View-Controller (MVC) This slide is based on: References
References • inheritance, abstract, interfaces, and polymorphism • John W. M. Russell’s Notes • Java Made Easy • The JavaTMTutorial • MVC • The JavaTM Tutorial • Web Presentation Patterns • Java SE Application Design With MVC
Writtentest • similar to writtentest #1 and writtentest #2 • 10 true/false = 10 marks • 10 short questions = 10 marks • explanations/procedures = 10-20 marks • methods, etc. = 30-40 marks • total = 60-80 marks
Labtest • will be combination of several topics that we have discussed: • GUI X recursion • linked lists/arrays X recursion • linked lists/arrays X GUI • etc. • practice: • PExs • exercises from the recommended book • examples from the lecture notes and the slides