Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods."— Presentation transcript:

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

2 Exam 1

3 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)

4 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

5 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)

6 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

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

8 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

9 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”);

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

11 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)

12 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

13 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();

14 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)

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

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

17 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

18 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

19 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

20 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

21 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

22 Promotion Rules

23 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

24 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; }

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

26 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)

27 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

28 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

29 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

30 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


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

Similar presentations


Ads by Google