SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler

Slides:



Advertisements
Similar presentations
the java language BY SA introduced in 1995 by Sun Microsystems.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
EECE 310: Software Engineering Lecture 2: Understanding Objects in Java and Types.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
Java Implementation: Part 3 Software Construction Lecture 8.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Types in programming languages1 What are types, and why do we need them?
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Sadegh Aliakbary Sharif University of Technology Spring 2011.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Programming Language Concepts (CIS 635)
Continuing Chapter 11 Inheritance and Polymorphism
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Java Programming Language
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Java Programming Language
Subtype Substitution Principle
Presentation transcript:

SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler

Announcements Tutorials start this Friday Tutorial/Lab location Still TBD – watch web page Additional tutorial slot Fridays 16:45 to 17:35 in H-929

OO Review OO programming Java: class, interface, ‘static’ Static and dynamic typing Polymorphism, delegation exceptions OO development using UML models

Java – some details to be read on your own. Primitive types: include –Floating point : float, double. –Integral : byte,short,int,long,char. –boolean, is not assignment compatible with integral types; explicit conversion required. boolean b = (i == 0 ? false : true); Class types for “boxing”: –Float, Double, Byte, … Boolean. –E.g. Boolean o = new Boolean(b);

java.util.Vector Vector class –Implements a growable array of objects. Example of use String s = …; Vector v = new Vector(); v.add(s); // adds to end String t = v.elementAt(0); v.remove(s); int i = v.size();

Java Basics – Hello World! public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld!"); } Illustrates: –How to write the main method to be called when a program is invoked. –Output (console).

Object and Variables: Run-time Initialization Local variables: –Are not implicitly initialized. –Must be explicitly initialized. Object fields always initialized by default to –Integral types (including char): 0. –Floating point types: 0.0. –Reference types: null.

Object Creation and Variable Declarations Basic declaration (no object creation): Animal a; null initialized declaration (no object creation): Animal a = null; Only using new will create (instantiate) objects a = new Duck();

Objects and Variables: Key Points What you should know: Run-time memory model –Has two parts: Stack, Heap. –You should know what gets allocated where. Object creation and variable declarations. –Declarations do not cause objects to be created. –Know the role of null. –Object creation / instantiation: via new.

Stack and Heap: Quiz Question Given the following declarations int i = 2; int[] b = new int[2]; String r; String s = “abc”; String t = null; What will be the state of the stack and heap after they have all been processed?

Stack and Heap Stack Heap ibrstibrst 2 ? null [0,0] “abc”

Disjointedness of Primitive and Reference Types You cannot assign a primitive type to a variable of a reference type. You cannot assign a reference to a variable of a primitive type. The remaining few slides discuss only reference types.

Type Hierarchy Every class is a subclass of Object. If S is a subclass of T then we can use an instance of S where ever a T is expected: T t = new S(); Object o = new S(); // Do not do this (it is wasteful): Object o = new String(“abc”);

Each Reference Variable: Two Types Each variable of a reference type has Declared type – fixed. Run-time type – can change at run-time. Synonyms: –D–Declared type: static type, or apparent type. –R–Run-time type: dynamic, or actual type.

Each Variable: Two Types, Example Object m = new Movie(); m has two types: –Declared type: Object. –Run-time type: Movie.

Type Hierarchy: Object, root of all Object A B X K

Type Hierarchy: Supertypes, Subtypes Object A B X K Supertypes of A Subtypes of A

Unrelated Sub-hierarchies are Not Compatible (for assignment, cast). Object A B X K Cannot –Assign, or –Cast A to K

‘Declared Type’ Fixes Bounds Declared type – fixed, e.g. A. Run-time type – can change at run-time... –w–within bounds of declared type subhierarchy. A B X

Type Checking: Assignment Always done relative to declared type. A a = some-expression-of-type- X Legal? (or will cause a compile-time error?) Assignment is legal iff X is –A, –Subtype of A. A B X

Type Casting and Type Checking I A a = (X) some-expression-of-type- K; Legal? (will cause a compile-time error?) Same answer as given on previous slide because this is just a special case of the previous slide.

OO Basics: Static vs. Dynamic Typing Why not declare all fields or variables of reference types as –Object –void * (C++) What would we gain? What would we loose?

Type Casting (in Statically Typed OO Languages) Purpose of Casting –Inform compiler of (assumed) subtype of an object. –Compiler can then perform better type checking. Type cast –Like an assertion, it may fail; e.g. Object i = new Integer(0); String s = (String) i; //  ClassCastException

Polymorphism “poly” – many “morphism” – forms How does the meaning of this term apply to OO? –Run-time type of a given expression can vary. Different types: our concern –Subtype polymorphism.

Number of Legs, Another Solution class Animal { protected int numberOfLegs; public int getNumberOfLegs() { return numberOfLegs; } } class Duck extends Animal { public Duck() { numberOfLegs = 2; } … } Issues? Best solution?

Dynamic Dispatching … Run-time type is only of concern when resolving non-static methods. Never for fields. Not for static methods.

Overridden Non-static Field There are two fields (one in P, one in C). Moral: do not do this! P f : int C

Static Methods No overriding P.m(), C.m() are distinct methods, both accessible. P int m() C

Exceptions, An Example: Throwing public void m(String s) throws NullPointerException { if(s == null) { throw new NullPointerException(); }... }

Exceptions, An Example: Catching public void m2(…) { String t =...; try { m(t); } catch( NullPointerException e) { // t was null } finally { // code to exec no matter what }

Exception Class Hierarchy Two types of exceptions –Checked –Unchecked Checked –Must be listed in method declaration. –Must be caught – or run-time error is reported. Unchecked –No such restrictions. ExceptionError Throwable RuntimeExceptionSampleChecked

Exceptions Both –methods –constructors can throw exceptions.