1 / 22

Classes and Methods (Cont’d) (Some slides omitted or modified.)

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

will
Télécharger la présentation

Classes and Methods (Cont’d) (Some slides omitted or modified.)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Methods (Cont’d) (Some slides omitted or modified.)

  2. 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

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

  4. 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) {..}

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

  6. 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 ) {..}

  7. 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

  8. Chapter 6 Object-Oriented Software Design and Implementation

  9. 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.

  10. 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

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

  12. 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

  13. 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

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

  15. Blank CRC Card Class Name: Superclass: Subclasses: Responsibilities Collaborations

  16. 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

  17. 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

  18. 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

  19. Address object inheritance hierarchy Object Address HomeAddress CompanyAddress WorkAddress BoxAddress

  20. Package Syntax Compilation Unit packageIdentifier ; ImportDeclaration . . . ClassDeclaration . . .

  21. 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

  22. Package Example • package addressBook • Members: • class Address • class Entry • Imported by • class AddressDr • All of the variables have package access

More Related