Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE212 – Reminders Assignment 3 due next Friday.

Similar presentations


Presentation on theme: "CMPE212 – Reminders Assignment 3 due next Friday."— Presentation transcript:

1 CMPE212 – Reminders Assignment 3 due next Friday.
Winter 2019 CMPE212 4/9/2019 CMPE212 – Reminders Assignment 3 due next Friday. Quiz 2 next week. See next 2 slides for topics. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Quiz 2 Topics Everything up to and including Friday’s material:
Quiz 1 Java topics, but not Java History or Background (“How Java Works”). System, String, StringTokenizer, Wrapper classes. Method Overloading. Catching, Building and Throwing Exceptions. Aliasing Objects, Passing by Reference. 2D Arrays. Objects in general. Instantiation. Encapsulation – private attributes, constructors, accessors, mutators and other standard methods. Exercises 1 to 8. Winter 2019 CMPE212 - Prof. McLeod

3 Quiz 2 Topics, Cont. Reflection will *not* be on the quiz.
File I/O from Exercise 4 – concepts only – you won’t have to write file I/O code. JUnit Testing – concepts only – you don’t need to write testing code. TDD. Packages & Java Modules – concepts only. Enumerated Types. Inner, Anonymous and Abstract Classes. Reflection will *not* be on the quiz. You will be given a list of String methods if you need them. One coding problem is likely. Otherwise, same rules as Quiz 1. Winter 2019 CMPE212 - Prof. McLeod

4 Today Aside - Using enums to limit legal arguments.
Winter 2019 CMPE212 4/9/2019 Today Aside - Using enums to limit legal arguments. static Inner Classes. Using these to limit legal arguments! Aside – Invoking a private method in a private inner class from another class using Reflection! Anonymous and Abstract Classes. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

5 Aside – Using enums to Limit Arguments
Winter 2019 CMPE212 Aside – Using enums to Limit Arguments Until now, constructors and mutators have been checking arguments for legality and throwing exceptions if they are not legal. Is it possible to restrict argument choices to only legal ones and thus bypass all the error checking and exception throwing stuff? Prevention instead of Repairing? Can you restrict the parameter type to an enum type? See LegalPizzaChoices.java, SimplePizza.java and TestSimplePizza.java. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

6 Aside – Using enums to Limit Arguments, Cont.
Winter 2019 CMPE212 Aside – Using enums to Limit Arguments, Cont. There is no way to provide illegal arguments for parameters that are of an enum type – you can only choose a member from the enum. If the parameter was of type String, for example – then you would need to check for legality of the argument with conditional(s). (It would be OK to use lower case enum members here…) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

7 Aside – Using enums to Limit Arguments, Cont.
Winter 2019 CMPE212 Aside – Using enums to Limit Arguments, Cont. But, what if you have an int type attribute where the legal values could lie between 1 and 100? You are not going to build an enum with 100 members! For this situation you must go back to the normal error checking and exception throwing mechanism. (In Assn 5, you can limit choices through GUI design instead…) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

8 Aside – Using enums to Limit Arguments, Cont.
Winter 2019 CMPE212 Aside – Using enums to Limit Arguments, Cont. And what if you still have a limited number of choices, but they are all numeric – and you will need the numeric value for a calculation? You cannot assign numeric values of your choice to enum members. You could go back to the normal error checking/exception throwing model, but: How about building a public static nested class containing a bunch of constants? You will also need a private attribute for the number, a constructor and (for convenience) a toString method. But, what is a static nested class?: Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

9 public static Inner Classes
Winter 2019 CMPE212 4/9/2019 public static Inner Classes What is the point of declaring an inner class public static? It would allow you to “categorize” methods into topical groups. Invoke them as below: First: TestClass tc = new TestClass(); Then, invoking methods from public static inner classes: tc.Group1.method(); tc.Group2.anotherMethod(); Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

10 Aside – Using static Inner Class to Limit Arguments
Suppose we want pizza size in inches because we are going to end up needing this number to calculate the cost of the pizza? How can you get the pizza size in inches from the user’s choice of size? (Assume that you cannot use the ordinal value.) How do you continue to limit argument choices to legal sizes? See LegalPizzaChoicesV2.java, SimplePizzaV2.java and TestSimplePizzaV2.java. Winter 2019 CMPE212 - Prof. McLeod

11 Using Reflection with Inner Classes
Winter 2019 CMPE212 4/9/2019 Using Reflection with Inner Classes See the demo program InnerClassDemo.java, used with TestInnerClassDemoV2.java. There is a way to invoke the private method in the private inner class from outside the class using Reflection. (You can prevent this type of misbehaviour through the module descriptor or by using security manager objects. I won’t get into this…) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

12 Using Reflection with Inner Classes
Winter 2019 CMPE212 4/9/2019 Using Reflection with Inner Classes Reflection is a set of techniques that allow you to discover and use any & all public/private/protected members of class through a Class<T> object. The demo also uses wildcarded generic types, the .class attribute, Method, Constructor and Class objects, as well as “multi-catch” try/catch blocks. Was this easy to write? NO!!! Should you do this stuff? NO!!! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

13 Anonymous Classes, Example
public class AnonymousClassDemo { public static void main(String[] args) { MessageSender ms = new MessageSender() { public void sendGreeting (String name) { System.out.println("Hello " + name + "!"); } // end sendGreeting }; // end ms, NOTE ";"! ms.sendGreeting("Alan"); } // end main } // end AnonymousClassDemo Winter 2019 CMPE212 - Prof. McLeod

14 Anonymous Classes, Example - Cont.
Displays “Hello Alan!” to the console (Wow!) MessageSender is an interface, not an Object: public interface MessageSender { void sendGreeting (String name); } // end MessageSender interface So, you have not actually named the Object that contains the definition of the method sendGreeting()! ms is an Anonymous Object. Winter 2019 CMPE212 - Prof. McLeod

15 Anonymous Classes, Cont.
Some coders will tell you that anonymous classes are just classes written by lazy coders. This is true, but you will see these little beasties used with GUI coding… (especially by lazy GUI coders, or the GUI wizards in NetBeans and Eclipse). Java 8 (and newer) now has a very tidy solution to using messy anonymous classes in a GUI program – by using Lambda Functions instead – more on these later! Winter 2019 CMPE212 - Prof. McLeod

16 Anonymous Classes, Example - Cont.
How would you do this is a “non-anonymous” way? First, create a named class that implements the interface: public class ImplementingSender implements MessageSender { public void sendGreeting(String name) { System.out.println("Hello " + name + "!"); } Winter 2019 CMPE212 - Prof. McLeod

17 Anonymous Classes, Example - Cont.
Then, instantiate this class and invoke the method: public class TestSender { public static void main(String[] args) { ImplementingSender iAmNamed = new ImplementingSender(); iAmNamed.sendGreeting("Alan"); } Now, iAmNamed is an instance of a named type. Winter 2019 CMPE212 - Prof. McLeod

18 Next… Interfaces are completely abstract – they cannot contain much in the way of concrete code. More shortly. But first, let’s look at something in-between an interface and a fully concrete class – an abstract class: Winter 2019 CMPE212 - Prof. McLeod

19 public abstract class MyClass …
Winter 2019 CMPE212 abstract Classes It is not unusual to declare a class in the root of a hierarchy to be abstract: public abstract class MyClass … Any class declared this way cannot be instantiated. It can only be extended. Unlike an interface, an abstract class can also contain concrete method definitions and any kind of attribute. If a class has one or more abstract methods, the class must be declared abstract as well. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

20 public abstract String getListing();
abstract Classes, Cont. abstract methods have no code in them. For example: public abstract String getListing(); A class that extends an abstract class must override all the abstract methods in the class, unless it wants to be abstract too. Unlike the interface, you should write public abstract for each method signature. Winter 2019 CMPE212 - Prof. McLeod

21 abstract Classes, Cont. Why bother?
An abstract class forces sub-classes to define certain methods. This helps ensure that the hierarchy has a consistent design. Also, when declaring a method in a very abstract class, then you don’t have to worry about what to do in the method body, especially if it must return a value. Can provide the mechanism for polymorphism! Winter 2019 CMPE212 - Prof. McLeod

22 Aside – Polymorphism (A Quick Peek)
We will learn more about this important aspect of OOP later, but for now: Polymorphism is when a pointer of a parent class type ends up pointing to different child class objects at runtime. Also called Dynamic Binding. The process must also satisfy early binding: Early Binding is satisfied when the parent class also owns the method that will end up being invoked from the morphed child class objects. The use of interfaces and abstract classes can make for easier coding to satisfy early binding. Winter 2019 CMPE212 - Prof. McLeod


Download ppt "CMPE212 – Reminders Assignment 3 due next Friday."

Similar presentations


Ads by Google