Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Design Bina Ramamurthy SUNY at Buffalo.

Similar presentations


Presentation on theme: "Object-Oriented Design Bina Ramamurthy SUNY at Buffalo."— Presentation transcript:

1 Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

2 Topics of Discussion zOO Principles zClass diagrams zProgram Structure zDefining methods zReturn statements zParameters zDefining classes zModifiers zSummary

3 Object-Oriented Principles OOP Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Virtual functions -- Abstract Base Classes

4 Java Class Diagram Data Declarations Methods EXAMPLE :Rectangle Length Width Color Area Perimeter Display

5 Examples of class and object Class : Doll Object Reference : Doll MyDoll; ; Object Instantiated: MyDoll = new Doll( ); Semantics (meaning) of “new” : create an object of the class specified and return its reference. Class : Rose Object References : Rose YourRose, HerRose; Object Instantiated : YourRose = new Rose(); One More Object Instantiated: HerRose = new Rose();

6 Examples Class Rose HerRose YourRose class objects Object References

7 Program Structure zA simple Java application program is a class with at least one method called “main” zmain method must always be defined using the words public, static, and void. zmain method is where the processing begins in a Java application program. zmain contains statements to be executed. zFile name and a program class name should be same in the environment you are working.

8 Method Invocation/Call zSyntax: method_name (values); object_name.method_name(values); classname.method_name(values); Examples: YourRose.PaintIt(Red); HerRose.PaintIt( Math.abs(RanGenerator.nextInt());

9 Defining Methods zA method is group of (related) statements that carry out a specified function. zA method is associated with a particular class and it specifies a behavior or functionality of the class. zA method definition specifies the code to be executed when the method is invoked/activated/called.

10 Method Definition : Syntax return_type method_name (parameter_list) { statements } 1. return type, 2. method_name to call it by 3. parameter list There is one more detail called “access control or visibility modifiers” associated with methods. We will discuss it later.

11 Return Type zcan be void, type or class identifier zvoid indicates that the method called to perform an action in a self-standing way: Example: println ztype or class specify the value returned using a return statement inside the method.

12 Return Statement zSyntax of return statement: return; // for void methods return expression; // for type or class return value // the expression type and return type should be same

13 Parameter List zParameter list specified in method header provides a mechanism for sending information to a method. zIt is powerful mechanism for specializing an object. zThe parameter list that appears in the header of a method yspecifies the type and name of each parameter and yis called formal parameter list. zThe corresponding parameter list in the method invocation is called an actual parameter list.

14 Parameter list : Syntax zFormal parameter list: This is like molds or templates (parm_type parm_name, parm_type parm_name,....) zActual parameter list: This is like material that fit into the mold or template specified in the formal list: (expression, expression....)

15 Method Definition : review return typename parameter list { statements } header body definition modifiers

16 Method Definition : Example zWrite a method that computes and returns the perimeter of a rectangle class. zAnalysis: ySend to the method: Length and Width yCompute inside the method: Perimeter yReturn from the method: Perimeter

17 ...Example (contd.) zname : perimeter zreturn type : int zparameters : int length and int width zcomputation :2 X (length + width) int Perimeter (int Length, int Width) { int Temp; // local temporary variable Temp = 2 * (Length + Width); // compute //perimeter return Temp; // return computed value }

18 What happens when a method is called? zControl is transferred to the method called and execution continues inside the method. zControl is transferred back to the caller when a return statement is executed inside the method.

19 Method Invocation : semantics 8 Main method Operating System Rect.Area(….) Area method 1 2 4 56 1. OS to main method 2. Main method execution 3. Invoke Area 4. Transfer control to Area 5. Execute Area method 6. Return control back to main method 7. Resume executing main 8. Exit to OS 3 7 8

20 Defining Classes zSyntax: zclass class_name { z data-declarations z constructors z methods } zConstructors are special methods used for instantiating (or creating) objects from a class. zData declarations are implemented using variable and constant declarations.

21 Constructors zA Constructor is used to create or instantiate an object from the class. zConstructor is a special method: yIt has the same name as the class. yIt has no return type or return statement. zTypically a class has more than one constructor: a default constructor which has no parameters, and other constructors with parameters.

22 Visibility Modifiers : private, public zEach member of a class whether it is a data or a method can be given access control or visibility modifier: zprivate : it means that the item is visible only within the class zpublic: means that the item is accessible from outside the class thru’ dot notation. zTypically the data members are private, most methods are public.

23 Classes and CLASSPATH zAll the classes needed for the application are placed in a file (or files). zThey can compiled separately. zApplication using the classes is placed in a file and compiled. zIf the classes are located in a different directory than the application CLASSPATH needs to be set to indicate the directories to search. zCLASSPATH =.:/util/lang/jdk1.1.6/lib/classes.zip:/u0/faculty/bi na/cs114a:{absolute path to your class repository}

24 Summary zClass definition and Object instantiation zMethod definition and invocation zDesign and implementation of methods and class


Download ppt "Object-Oriented Design Bina Ramamurthy SUNY at Buffalo."

Similar presentations


Ads by Google