UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
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.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Object Oriented Programming
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Topic: Classes and Objects
Java Primer 1: Types, Classes and Operators
Nested class.
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Java Inheritance.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Java Programming Language
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Presentation transcript:

UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods Mon. 9/25/00

Exam 1

Homework #3 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date. Homework #4 will be assigned on Fri., 10/6 (skipping one week due to test)

Inheritance ä An instance of a subclass can be treated as an instance of the superclass ä A reference to a superclass can be assigned to an instance of a subclass ä A reference to a subclass can be implicitly converted to a reference to a superclass ä A reference to a superclass can be cast to a reference of a subclass ä A ClassCastException might be thrown

Inheritance vs Instantiation ä Inheritance is a relationship among types of objects (classes), not among objects themselves ä A Sedan is-a Car ä Instantiation is a relationship between a class and an object ä myCar is an object which is an instance of the class Car ä Instantiation also implements an “is-a” relationship ä myCar is-a Sedan (which is-a Car)

public Access ä class - a public class can be accessed outside of its package ä package acts as “container” for classes ä interface - a public interface can be accessed outside of its package ä members (data and methods) - public members can be accessed anywhere its class can be accessed

protected Access ä members - protected members can only be accessed within its class or from a (direct) subclass

private Access ä class - a private class can be accessed only in its package ä members - private members can only be accessed within its class ä Subclass cannot directly access private members of superclass ä Can use a protected or public accessor method, if one exists

Class (static) Methods ä Global to the class ä Provide some general utility related to the class ä static methods can not access instance variables ä static methods can be called even if no class instances exist (e.g., create a String from another type) ä Examples: class Math, wrapper classes (Integer, Float, Boolean, Double) have static methods ä e.g. Double.parseDouble(“5.3”);

static initialization ä Executed upon “loading” of the class ä Occurs before main is invoked ä Allows for complex initialization of static variables ä Syntax: static { // do stuff }

Final ä Final: ä Class: final in a class declaration prevents a class from being extended ä Member methods: final prevents a subclass from overriding the method ä Variables: initialize to a value when declared; cannot change value later (similar to C/C++ const) “final” is different from “finalize” (which is like a destructor)

Calling (invoking) Methods ä Syntax: variable.methodName(argumentList) ä or, from another method of the same class: methodName(argumentList) ä or, for a static method: className.methodName(argumentList) ä The must be one argument of the appropriate type for each parameter in the method definition ä More shortly

Handling Return Values ä If the method returns a value ä Can assign the return value to a variable ä Can include the method invocation as an argument to another method invocation ä If the return value is itself an object that has methods, can chain method invocations ä myObject.getClass().getName();

Control Flow with Method Invocation ä Control passes into the method upon invocation ä Control returns to the calling location when: ä ä The last statement in the method is executed, or ä By executing a return statement ä ä Return values (if any) are specified after the return keyword ä A method implementation can have several return points Java supports recursion (see later slide)

Overloading Methods ä Same method name ä Different argument list (different order of types) ä Cannot differ by return type only

Duration & Scope of Variables ä Duration - how long the variable’s storage exists ä Scope - from where in the code can you refer to the storage

Duration ä A method’s local variables live from the point of definition to the end of the block in which they are defined ä A method’s parameters live for the duration of the method invocation ä Instance variables live as long as the instance lives ä static (class-level) variables live from the point of definition for the duration of the execution ä Variables must be declared and initialized before used ä Local variables do not get a default initial value

Scope ä A method’s local variables can be referenced from the point of definition to the end of the block in which they are defined ä A method’s parameters can be referenced in the method ä Instance variables can be referenced in any method of the class or (if public) through any reference to an instance of the class

Scoping Order in Java ä Current block ä Subsequent outer blocks ä Current method definition (local variables, then parameters) ä Instance variables in current class ä Instance variables in super classes

Java Catches Certain Scope Problems ä If an outer block declares a variable of the same name as an inner block, the compiler will complain ä But, a local variable (or method parameter) can “hide” an instance variable of the same name without warning

Automatic Conversion of Arguments ä Argument values that aren’t of the precise type as the corresponding parameter can occasionally be converted for you ä Uses Java’s type promotion rules

Promotion Rules

Explicit Type Conversions - Casting ä Can convert/cast ä between primitive types (e.g., float to int) ä between an instance of one class and an instance of another ä between primitive types and class instances

Casting between Primitive Types ä Cannot cast from boolean to others ä Can cast from 1 or 0 to boolean ä Can lose information if go from a more precise to a less precise type (e.g., float to int) ä Syntax: (typeName) variable ä Example: { float a = 1.234; int i = (int) a; }

Converting between Objects ä Can cast from a subtype instance to a supertype instance ä Will cover in more detail in the discussion of inheritance

Converting Primitives to Objects (and vice-versa) ä No automatic cast or conversion ä Requires use of “wrappers” or special conversion methods ä Can also use ad hoc conversion methods (generally static methods) ä e.g., int Integer.parseInt(String)

Java Supports Recursion ä ä Some algorithms are more easily expressed via recursion (vs iteration) ä ä A method can (directly or indirectly) call itself ä ä Care must be taken ä ä Recursion can consume the stack with copies of local variables ä ä Recursion can execute more slowly than iteration due to more method-call overhead

Definition - Interface ä An interface defines a collection of methods (is a collection of method signatures) ä A class implements an interface ä An interface can also define constants ä Interfaces are abstract (all their method declarations are abstract) ä Interfaces can be imported (like packages) ä Syntax: modifiers interface InterfaceName extendsClause { // interface body } ä More later

Nested Classes ä Nested class = class within a class ä Scope of nested class is bounded by scope of enclosing class ä Nested class has access to members (even private) of enclosing class ä Enclosing class does not have access to members of nested class ä Static nested class: not often used ä Inner class (non-static): used often for GUIs to handle events ä We’ll use these for GUIs and also for Jini

Inheritance vs Nesting ä Scope: ä A non-static nested class is known only within scope of enclosing class ä A subclass can be known outside its superclass ä Access: ä A nested class can access members (even private ones) of enclosing class ä A subclass cannot directly access private members of its superclass