Download presentation
Presentation is loading. Please wait.
Published byFelix Douglas Modified over 9 years ago
1
"If I can't picture it, I can't understand it."
2
Reference anonymous - gravity https://www.youtube.com/watch?v=cEkILY1h6fA Concrete classes: rules of fight club https://www.youtube.com/watch?v=vJMC_S-DB2I Type anonymous : Fight Club - Robert Paulson https://www.youtube.com/watch?v=GCi_PIz5ekU Dubugger: x-men quicksilver https://www.youtube.com/watch?v=WGDXO9mlprM
3
References Objects are like astronauts.
4
References Object objDate = new Date(); This date has a reference. The reference is an Object.
5
Reference Anonymous MyTime myTime = new MyTime(new Date()); The date is reference-anonymous, but we can still get a reference to it myTime.getDate(); new Date(); The date is reference-anonymous and we have no reference to it – it is space-junk and will be garbage collected.
6
Reference Anonymous Date dat1 = new Date(); Date dat2 = new Date(); dat1 = dat2; The Object originally stored in dat1 is orphaned and becomes space junk.
7
Java entityMay be a reference?May be an instantiated? Concrete ClassYES Abstract ClassYESNO InterfaceYESNO https://www.youtube.com/watch?v=vJMC_S-DB2I
8
Fight Club Rules of Java 1/ Only concrete classes can be instantiated.
9
Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated.
10
Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated. 3/ The type of any Object must be of type Concrete_class. (corollary to 1 and 2) 4/ References may be concrete, abstract, or interface.
11
Fight Club Rules of Java 5/ You may create type-anonymous abstract- classes and interfaces by overriding ALL their contract methods when you declare them. 6/ The “type” of a type-anonymous class is $x aka Robert Paulson.
12
Reflection If your program is written well and adheres to the principals of polymorphism, then you don't really need reflection. However, it's nice to know you have it when testing/debugging though. Very useful when first learning an OO language.
13
Reflection Reflection allows you to inspect the type (class) of the implicit parameter at runtime. We will use reflection to gain a deeper understanding of polymorphism and the java event model. Every class has a class object which you can access like so: java.util.Date.class, or like so: Class.forName(strFullyQualifiedClass); See reflection example
14
Reflection Name of Driverimplemnts Implicit param EventListener typeDefined TimeTestOuter ActionListener yesEventListenerOuter In separate java file TimeTestInner ActionListener yesEventListenerInnerIn same java file TimeTestLocal ActionListener yesanonymousSame method TimeTestAnon ActionListener noanonymousinline See inner example
15
Java Event Model Used to program behavior in GUIs Used extensively in Android.
16
If a tree falls in a forest and no one is around to hear it, does it make a sound?
18
Event (onClick) No Event-Listener listening No Catcher Event-Source (Button) No Event Listener
19
Event (onClick) Event-Listener listening Catcher ready to catch Event-Source (Button) ActionList ener Any Object
20
Wrong Event
21
Event source not registered
22
Event (Action) Event-Listener listening Catcher ready to catch Action- Listener Event-Source (Button) Any Object OnMouse- Listener Event (Mouse)
23
Step 1/ define the event-listener object and override the appropriate methods ActionListener mActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //behavior here }}; Step 2/ register (add) the event-listener to the event source. mButton.addActionListener(mActionListener);
24
http://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html http://docs.oracle.com/javase/tutorial/uiswing/events/api.html
25
Casting Casting down may be done ONLY down the class hierarchy. Casting up is not required because a subclass object may always be stored in a superclass reference (or an interface that it implements).
26
Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and write procedural-like code. Often times, no one but the enclosing class cares about an object. In this case, you may consider using inner or anonymous classes.
27
Write a very simple application for a contact manager. The the sake of simplicity, each contact will have a name and a phone number only. The user should be able to create new contacts and diplay all contacts. Create a Latin dictionary with entries for Latin and English equivalents. Store them in a list and allow the user to delete them.
28
Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class. An interface is a contract; it defines the methods
29
The ColorSelector GUI App
30
The Leet Translator GUI App
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.