220 likes | 338 Views
Classes and Methods (Cont’d) (Some slides omitted or modified.). Method Signature. The signature of a method consists of the method’s name , together with the number and type(s) of its parameters in their given order
E N D
Classes and Methods (Cont’d) (Some slides omitted or modified.)
Method Signature • The signature of a method consists of the method’s name, together withthe number and type(s) of its parameters in their given order • Method overloading is the use of a method name more than once, each time with different signatures
Method Overloading Means • Several methods of the same name are defined with different sets of parameters (based on number of parameters, types of parameters, or order of parameters) • Multiple methods can share the same name if at least one of these conditions is met: • 1. They have different NUMBERS of parameters • 2. The parameters are different datatypes
Method Overloading Examples • These methods are overloaded: public void someMethod (int a, int b) {….} public void someMethod (int y) {..} • These methods are overloaded : public void someMethod (int a, int b) {….} public void someMethod (int a, double b) {..} • These methods are NOT overloaded : public void someMethod (int a, int b) {….} public double someMethod (int a, int b) {..}
Constructors The default constructor has no parameters and no statements in the body: public Student ( ) { } • If no constructor is defined for a class, Java compiler will automatically add the default • Do not rely on default; always define your own
Constructor Overloading Examples • Multiple constructors are often defined for an object-creating class. • Overloaded Constructor with one parameter: public Student (String inputName ) {..} • Overloaded Constructor with two parameters: public Student (String inputName, int inputID ) {..}
Object State • The object’s state is the current set of values that it contains • An object is instantiated with an initial state. If any of its methods can subsequently change its state, the object is said to be mutable • If there are no methods that can change an object’s state, it is said to be immutable (Ex.: String object) • Methods that change an object’s state are called mutators or transformers
Chapter 6 Object-Oriented Software Design and Implementation
Software Design Strategies FUNCTIONALOBJECT-ORIENTED DECOMPOSITIONDESIGN The problem is divided into more easily handled subproblems, the solutions of which together create a solution to the overall problem. Produces a hierarchy of tasks. The solution is expressed in terms of objects (self-contained entities composed of data and operations on that data) that interact by sending messages to one another. Produces a hierarchy of objects.
Object-Oriented Design (OOD) • Focus is on the entities (objects) in a problem • Beginsby identifying the classes of objects in the problem, and choosing appropriate operations on those objects • Programs are collections of objects that communicate with (send messages to) each other • Data plays a leading role; algorithms are used to implement operations on the objects and to enable interaction of objects with each other
OOD good with large software projects • Objects within a program often model real-life objects in the problem to be solved • The OOD concept of inheritance allows the customization of an existing class to meet particular needs. This can reduce the time and effort needed to design, implement, and maintain large systems
Object-Oriented Design Process • Three steps in the process • Identify an initial set of object classes that seem relevant to the problem • nouns often represent objects • verbs often represent actions • Filter the list, eliminating duplicates • Identify the responsibilities for the reduced list of objects
Identify Possible Classes • Brainstorming (by a team) • Identify objects • Propose classes • Write on a blackboard • Keep going around until no one can think of any more objects • No ideas are rejected
Filter List of Possible Classes • Eliminate duplicates • Decide if the classes really do represent objects in the problem • Look for classes that are related • Over looked classes may emerge • For each class that survives the filtering stage, create a CRC card
Blank CRC Card Class Name: Superclass: Subclasses: Responsibilities Collaborations
Definitions • Responsibilities An action that an implementation of an object must be capable of performing • Collaboration An interaction between objects in which one object requests that another object carry out one of its responsibilities
Determine Responsibilities • Initial responsibilities • Know and return the states of the object • Called Knowledge Responsibilities • Action responsibilities Use scenario walk-throughs, a role playing technique, • to explore the actions of a class • to explore the interactions of classes
Inheritance • Inheritance A mechanism by which one class acquires (inherits) the properties (both data fields and methods) of another class. • Enables us to define a new class by adapting the definition of an existing class • Superclass The class being inherited from • Derived class The class that inherits • The derived class is specialized by adding properties specific to it
Address object inheritance hierarchy Object Address HomeAddress CompanyAddress WorkAddress BoxAddress
Package Syntax Compilation Unit packageIdentifier ; ImportDeclaration . . . ClassDeclaration . . .
Package Do’s and Don’t’s • A compilation unit can have only one public class • Many compilation units can be in a package • “No modifier” specifies package access • Any field or method with package access can be accessed by any member of the package
Package Example • package addressBook • Members: • class Address • class Entry • Imported by • class AddressDr • All of the variables have package access