Upload
veronica-higgins
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
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 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: 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 beneficial for initializing properties
4 This slide is based on: References
Inheritance – Superclasspublic 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 – Subclassespublic 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: 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 – Superclasspublic 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: 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
9 This slide is based on: References
Abstract Method in Superclasspublic 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 – Subclassespublic 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 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: References
Interfaces – Superclass and Dogpublic 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 polymorphism1. overloading2. overriding3. late (or dynamic) method binding
14 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
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: 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!!
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 [Burbeck92]
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
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: 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
20 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
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
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: 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 JavaTM Tutorial
MVC The JavaTM Tutorial Web Presentation Patterns Java SE Application Design With MVC
25
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
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