Download presentation
Presentation is loading. Please wait.
Published byJeffry Gilbert Modified over 9 years ago
1
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon. 9/25/00
2
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.
3
Object-Oriented Design ä Identify “natural” objects ä Physical things (e.g., Airplanes, Airports) ä Abstract things (e.g., deliveries, elections) ä Identify the natural operations performed on those objects ä Identify the relationships among objects ä Design procedures and corresponding data simultaneously
4
Advantages of OOP ä Maintainability ä Implementation details are hidden and can be changed without informing clients ä Reusability ä Can inherit behavior without having to know implementation details
5
Definition - Class ä A “user-defined” (programmer-defined) data type ä Represents some real-world concept ä Similar to a C struct (structured data type, record, …) ä Generally includes both ä state (data - called instance variables) and ä behavior (related procedures) ä May or may not have instances (objects)
6
Encapsulation ä ä A class represents some (real-world) concept in one mechanism ä ä The class “encapsulates” the concept ä ä Includes both state (data) and behavior (methods) ä ä The methods "act on" the data
7
Data (Information) Hiding ä ä How a class is implemented is hidden from the user of the class ä ä State (data) is generally of no concern to the user of the class ä ä Only behavior (methods) is of concern ä ä An external user of the class interacts with it by invoking its public methods ä ä Data is often private ä ä Methods are generally public
8
Inheritance ä A class can inherit state (data) and behavior (methods) from another class ä A subclass inherits from (specializes) a superclass ä Adds to superclass data ä Adds to or modifies superclass methods ä [An instance of a subclass is generally larger than an instance of its superclass] ä Inheritance supports reuse ä Syntax: class subClass extends superClass
9
Inheritance (continued) ä Inheritance is hierarchical ä class C extends class B which extends class A (and so on) ä class C inherits from both A and B ä Implements an “is-a” relationship among classes ä A Rectangle is-a Polygon ä vs “has-a” relationships (implemented via composition) ä A Delivery has-a Plane
10
Inheritance (continued) ä 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
11
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)
12
Definition - Method ä A method is a procedure that is associated with a class ä A method generally acts on data that is associated with the class or an instance of the class ä Methods are used to interact with the class ä For example, to retrieve information about the class or an instance
13
Typical Methods of a Class ä Initialization (constructors) ä Cleanup (finalizers) ä Assignment ä Accessors (get, set) ä Converters ä Class logic ä Utilities
14
Constructors ä ä Special initialization method ä ä Same name as the class ä ä Can be overloaded ä ä Cannot return a value (not even void) ä ä If none specified, compiler creates a default constructor ä ä Good constructors to have: ä “No-argument” constructor ä Call default constructor for class that this class extends ä Initialize instance variables ä Copy constructor
15
Access Control ä Access can be restricted to a class, a class and its subclasses, or a package by using access modifiers ä Default access is package-level access ä No access modifier specified ä Classes, class members (methods, variables), interfaces are freely available by a class or interface within the same package
16
public Access ä class - a public class can be accessed outside of its package ä interface - a public interface can be accessed outside of its package ä members - public members can be accessed anywhere its class can be accessed
17
private Access ä class - a private class can be accessed only in its package ä interface - does not apply? ä members - private members can only be accessed within its class
18
Access from within a Method ä ä Methods can only reference data that are arguments (copied into parameters), locally- declared variables, instance-variables, static (class-level) variables, and data that is exposed via some other class’s public interface ä Methods can only invoke the methods of its own class (to include methods inherited from parent classes) as well as the methods that are exposed via some other class’s public interface (only if it has a reference to an instance of the other class for non-static methods)
19
More about Methods ä Methods have a signature ä Specifies its name, return type, and the number, names, and types of its arguments ä Methods can be static or “non-static” ä (Explicit) static methods are “class methods” ä (Implicit) Non-static methods are “instance methods”
20
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) have static methods
21
static initialization ä Executed upon “loading” of the class ä Occurs before main is invoked ä Allows for complex initialization of static variables ä Syntax: static { // do stuff }
22
Creating Methods ä Methods are declared inside of the class definition by specifying its signature and method body ä Syntax: modifiers returnType methodName(parameter list) throwsClause { // method body } ä Must include a returnType (does not assume int like C/C++) ä Cannot nest method definitions
23
Creating Methods - Hints ä Give methods meaningful names ä Give parameters meaningful names ä Limit the functional scope to a reusable meaningful piece of functionality
24
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
25
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();
26
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
27
Overloading Methods ä Same method name ä Different argument list (different order of types) ä Cannot differ by return type only
28
Duration & Scope of Variables ä Duration - how long the variable’s storage exists ä Scope - from where in the code can you refer to the storage
29
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
30
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
31
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
32
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
33
final Variables ä A variable declared as final cannot be changed after initialization ä Effectively creates a constant ä Can you have class-level final variables?
34
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
35
Promotion Rules
36
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
37
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; }
38
Converting between Objects ä Can cast from a subtype instance to a supertype instance ä Will cover in more detail in the discussion of inheritance
39
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)
40
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
41
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.