Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)

Similar presentations


Presentation on theme: "Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)"— Presentation transcript:

1 Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)

2 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: ReferencesReferences

3 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 static methods 3 This slide is based on: ReferencesReferences

4 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  beneficial for initializing properties 4 This slide is based on: ReferencesReferences

5 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: ReferencesReferences

6 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(); 6 This slide is based on: ReferencesReferences

7 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: ReferencesReferences

8 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(); 8 X This slide is based on: ReferencesReferences

9 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 9 This slide is based on: ReferencesReferences

10 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: ReferencesReferences

11 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: ReferencesReferences

12 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 12 This slide is based on: ReferencesReferences

13 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: ReferencesReferences

14 Polymorphism  allows an action or method to do different things based on the object that it is acting upon  three types of polymorphism 1. overloading 2. overriding 3. late (or dynamic) method binding 14 This slide is based on: ReferencesReferences

15 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: ReferencesReferences

16 Late Method Binding  it allows a program to resolve references to subclass methods at runtime  for instance, let’s assume  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 16 This slide is based on: ReferencesReferences

17 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!! 17 This slide is based on: ReferencesReferences

18 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] 18 This slide is based on: ReferencesReferences

19 Model-View-Controller (MVC)  model – manages the behaviour and 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 19 This slide is based on: ReferencesReferences

20 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 20 This slide is based on: ReferencesReferences

21 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 21 This slide is based on: ReferencesReferences

22 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 22 This slide is based on: ReferencesReferences

23 Model-View-Controller (MVC) 23 This slide is based on: ReferencesReferences

24 Model-View-Controller (MVC) 24 This slide is based on: ReferencesReferences

25  inheritance, abstract, interfaces, and polymorphism  John W. M. Russell’s Notes John W. M. Russell’s Notes  Java Made Easy Java Made Easy  The Java TM Tutorial The Java TM Tutorial  MVC  The Java TM Tutorial The Java TM Tutorial  Web Presentation Patterns Web Presentation Patterns  Java SE Application Design With MVC Java SE Application Design With MVC 25

26 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 26

27 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 27


Download ppt "Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)"

Similar presentations


Ads by Google