CMPE212 – Reminders Assignment 3 due next Friday.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Oriented Programming: Java Edition By: Samuel Robinson.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Coming up: Inheritance
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Topic: Classes and Objects
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
OOP’S Concepts in C#.Net
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC124 Assignment 4 on Inheritance due today at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 due tomorrow at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CMPE212 – Reminders Assignment 2 due this Friday.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
Winter 2019 CMPE212 4/20/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Chapter 8 Inheritance Part 2.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due in a week.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
CS 240 – Advanced Programming Concepts
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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