Summary prepared by Kirk Scott

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Design Patterns in Java Chapter 18 Prototype Summary prepared by Kirk Scott 1.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Java: Chapter 1 Computer Systems Computer Programming II.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
4.1 Instance Variables, Constructors, and Methods.
Unit 4 Prototype Summary prepared by Kirk Scott 1.
Unit 4 Prototype Summary prepared by Kirk Scott 1.
The Java Programming Language
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Appendix D UML at a Glance Summary prepared by Kirk Scott 1.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
OOP Basics Classes & Methods (c) IDMS/SQL News
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
External Scope CECS 277 Mimi Opkins.
Modern Programming Tools And Techniques-I
Classes and Objects.
Working with Java.
Summary prepared by Kirk Scott
Design Patterns in Java Chapter 23 Strategy
Class Structure 15-Jun-18.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Summary prepared by Kirk Scott
Classes and Objects in Java
Lecture 15: More Inheritance
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Review Session.
Lecture 9-2: Interacting with the Superclass (super);
Java Unit 11: Inheritance I
Generics, Lambdas, Reflections
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 4: Writing Classes
CSC240 Computer Science III
CSC 113 Tutorial QUIZ I.
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Recitation 7 October 7, 2011.
Classes and Objects in Java
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,
CLASSES, AND OBJECTS A FIRST LOOK
Lecture 15: Inheritance II
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
More on Creating Classes
Review of Previous Lesson
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 1
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Classes and Objects in Java
Visibilities and Static-ness
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Exceptions.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
CS 240 – Advanced Programming Concepts
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Summary prepared by Kirk Scott Design Patterns in Java Part III Construction Patterns Chapter 14 Introducing Construction Summary prepared by Kirk Scott

Standard construction means writing suitable constructors in a class definition Construction design patterns cover cases where a simple set of class constructors won’t satisfy the needs of a given application Before previewing the construction patterns, the book reviews construction in general

A Few Construction Challenges The book describes constructors a kind of specialized method Purists might disagree Like methods, constructors have access modifiers, overloading, and parameters However, what the constructor returns is fixed—and there is no return statement for the programmer to fiddle with There are a number of special syntactical and semantic aspects to constructors

Challenge 14.1 List four rules that govern the use and behavior of constructors in Java.

Solution 14.1 Special rules regarding constructors include the following. 1. If you do not supply a constructor for a class, Java will provide a default. 2. Constructor names must match the class name. (Thus, constructor names typically start with a capital letter, unlike most method names.)

3. Constructors can invoke other constructors with this() and super(), so long as this invocation is the first statement in the constructor. Comment mode on: The use of super() was covered in CS 202 The user of this() wasn’t However, there is nothing difficult about using this It simply lets one constructor make use of another

4. The “result” of a constructor is an instance of the class, whereas the return type of an ordinary method can be anything. 5. You use new, or reflection, to invoke a constructor Comment mode on: You might also observe that user class construction ultimately relies on construction in the Object class Stay tuned for a bit more information on reflection

There are more details that didn’t make it into the previous list Summarizing the whole mess: If you don’t supply a constructor, Java will provide a default constructor If your constructor doesn’t use this() or super(), by default, it is as if the first line of code in your constructor is super()

Most importantly, as a potential source of cryptic errors: If you do provide any constructor at all, Java no longer provides a default constructor If default constructor is needed, it has to be explicitly supplied by the programmer It’s easy to come up with clear examples of how this can cause problems

The book chooses to illustrate this with code that is not quite so simple On the following overhead code is given for: A superclass with a constructor A subclass (without a constructor) The code is problematic The code is followed by a challenge that asks for an explanation

public class Fuse { private String name; /* public Fuse(String name) this.name = name; } */ } public class QuickFuse extends Fuse

Challenge 14.2 Explain the error that will occur if you uncomment the lines that allow the Fuse superclass to accept a fuse name in its constructor

Solution 14.2 This is not a copy of the book’s answer, but it says the same thing The TextPad compiler gives this error message cannot find symbol symbol : constructor Fuse() location: class Fuse class QuickFuse extends Fuse

Solution 14.2, continued This message is telling you that there is no default constructor in the Fuse class There is none because a non-default constructor was supplied The system doesn’t supply one when the programmer provides any other constructor Plus, the programmer didn’t provide one

Solution 14.2, continued Why is a default Fuse class constructor needed? The programmer didn’t provide any constructor for the subclass, QuickFuse Therefore the system supplies a default constructor Even though the default constructor in the subclass isn’t visible, it relies on the existence of a default constructor in its superclass As noted, a default constructor doesn’t exist in its superclass Therefore, compilation fails

Reflection is that part of Java that allows a program to make calls like getClass() In other words, it’s possible for a program to “find out” what kinds of objects it’s working with Reflection makes it possible to find out many more things about an object than just what class it is an instance of

For example, it’s also possible to obtain the constructors for the class Even though reflection is only touched on sparingly in CS 202 and thus far in CS 304, the book provides a little example The code on the following overhead makes use of the getConstructors() method of reflection On the overhead after that a challenge asks what the code does

import java.awt.Point; import java.lang.reflect.Constructor; public class ShowReflection { public static void main(String args[]) Constructor[] cc = Point.class.getConstructors(); Constructor cons = null; for (int i = 0; i < cc.length; i++) if (cc[i].getParameterTypes().length == 2) cons = cc[i]; try Object obj = cons.newInstance( new Object[] { new Integer(3), new Integer(4) }); System.out.println(obj); } catch (Exception e) System.out.println("Exception: " + e.getMessage());

Challenge 14.3 What does the ShowReflection program print out?

Solution 14.3 The program prints: java.awt.Point[x=3, y=4] (It has successfully found a constructor that takes two arguments and created a new point with the arguments given.)

Summary From the point of view of standard construction: You write a set of constructors for your classes, making sure not to trip up on any rules In advance cases, you might use reflection, assuming you knew how… Note: As usual, with an introductory chapter, more stuff follows the summary

Beyond Ordinary Construction Ordinary construction works fine assuming that the client code programmer knows the following: Which class is an instance needed of? What are the construction parameters? Does the client code have values for the needed parameters in hand? If available, does the client code have the values in the right format?

The book tries to sketch out a scenario where ordinary construction might not be sufficient Suppose an application may run on either a desktop or a handheld device Which user interface components to construct, or which construction parameters to use might depend on the environment in which the application is currently running

The table on the next overhead previews the construction design patterns As usual, the authors try to explain and distinguish the patterns according to their intent, or purpose Each of these patterns is supposed to help make construction easier or more appropriate in a particular programming context

The End