If I cant picture it I cant understand

  • Slides: 31
Download presentation
"If I can't picture it, I can't understand it. "

"If I can't picture it, I can't understand it. "

Reference anonymous - gravity https: //www. youtube. com/watch? v=c. Ek. ILY 1 h 6

Reference anonymous - gravity https: //www. youtube. com/watch? v=c. Ek. ILY 1 h 6 f. A Concrete classes: rules of fight club https: //www. youtube. com/watch? v=v. JMC_S-DB 2 I Type anonymous : Fight Club - Robert Paulson https: //www. youtube. com/watch? v=GCi_PIz 5 ek. U Dubugger: x-men quicksilver https: //www. youtube. com/watch? v=WGDXO 9 mlpr. M

References Objects are like astronauts.

References Objects are like astronauts.

References Object obj. Date = new Date(); This date has a reference. The reference

References Object obj. Date = new Date(); This date has a reference. The reference is an Object.

Reference Anonymous My. Time my. Time = new My. Time(new Date()); The date is

Reference Anonymous My. Time my. Time = new My. Time(new Date()); The date is reference-anonymous, but we can still get a reference to it my. Time. get. Date(); new Date(); The date is reference-anonymous and we have no reference to it – it is space-junk and will be garbage collected.

Reference Anonymous Date dat 1 = new Date(); Date dat 2 = new Date();

Reference Anonymous Date dat 1 = new Date(); Date dat 2 = new Date(); dat 1 = dat 2; The Object originally stored in dat 1 is orphaned and becomes space junk.

Java entity May be a reference? May be an instantiated? Concrete Class YES Abstract

Java entity May be a reference? May be an instantiated? Concrete Class YES Abstract Class YES NO Interface YES NO https: //www. youtube. com/watch? v=v. JMC_S-DB 2 I

Fight Club Rules of Java 1/ Only concrete classes can be instantiated.

Fight Club Rules of Java 1/ Only concrete classes can be instantiated.

Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only

Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated.

Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only

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.

Fight Club Rules of Java 5/ You may create type-anonymous abstractclasses and interfaces by

Fight Club Rules of Java 5/ You may create type-anonymous abstractclasses 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.

Reflection • If your program is written well and adheres to the principals of

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.

Reflection • Reflection allows you to inspect the type (class) of the implicit parameter

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. for. Name(str. Fully. Qualified. Class); See reflection example

Reflection Name of Driver Time. Test. Outer Time. Test. Inner Time. Test. Local Time.

Reflection Name of Driver Time. Test. Outer Time. Test. Inner Time. Test. Local Time. Test. Anon implemnts Action. Listener See inner example Implicit param Event. Listener type Defined yes Event. Listener. Outer In separate java file yes Event. Listener. Inner In same java file yes anonymous Same method no anonymous inline

Java Event Model • Used to program behavior in GUIs • Used extensively in

Java Event Model • Used to program behavior in GUIs • Used extensively in Android.

If a tree falls in a forest and no one is around to hear

If a tree falls in a forest and no one is around to hear it, does it make a sound?

Event-Source (Button) No Event-Listener listening Event (on. Click) No Catcher No Event Listener

Event-Source (Button) No Event-Listener listening Event (on. Click) No Catcher No Event Listener

Event-Source (Button) Event-Listener listening Event (on. Click) Action. List ener Any Object Catcher ready

Event-Source (Button) Event-Listener listening Event (on. Click) Action. List ener Any Object Catcher ready to catch

Wrong Event

Wrong Event

Event source not registered

Event source not registered

Event-Source (Button) Event-Listener listening Event (Action) Action. Listener On. Mouse. Listener Any Object Event

Event-Source (Button) Event-Listener listening Event (Action) Action. Listener On. Mouse. Listener Any Object Event (Mouse) Catcher ready to catch

Step 1/ define the event-listener object and override the appropriate methods Action. Listener m.

Step 1/ define the event-listener object and override the appropriate methods Action. Listener m. Action. Listener = new Action. Listener() { @Override public void action. Performed(Action. Event e) { //behavior here }}; Step 2/ register (add) the event-listener to the event source. m. Button. add. Action. Listener(m. Action. Listener);

Casting • Casting down may be done ONLY down the class hierarchy. • Casting

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).

Inner and Anonymous Classes • Though you are captive in the OO paradigm, you

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.

Write a very simple application for a contact manager. The the sake of simplicity,

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.

Interfaces • A class implements an interface rather than extends it. Any class that

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

The Color. Selector GUI App

The Color. Selector GUI App

The Leet Translator GUI App

The Leet Translator GUI App