11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
The Java Programming Language
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA BASICS Prepared by The Smartpath Information Systems
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Introduction to Object-Oriented Programming Lesson 2.
Creating a Java Application and Applet
Classes, Interfaces and Packages
Object-Oriented Design Bina Ramamurthy SUNY at Buffalo.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-Oriented Design
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
B.Ramamurthy Copyright 1998 CSE Department
Introduction to Object-oriented Program Design
Object-oriented Design in Processing
Object-Oriented Programming
Problem Solving, Object-Oriented Design and Java
Object-Oriented Programming
Object-oriented Design in Processing
Problem Solving, Object-Oriented Design and Java
Object-Oriented Programming
B.Ramamurthy Copyright 1998 CSE Department
Object Oriented Programming in java
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Object-oriented Design in Processing
Final and Abstract Classes
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Object-oriented Design in Processing
Presentation transcript:

11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy

11/28/2015B.Ramamurthy2 Topics for Discussion OO Design Principles Java Virtual Machine Java Application Structure Class and objects Methods and Variables Access/Visibility Modifiers Summary

11/28/2015B.Ramamurthy3 Object-Oriented Principles OOP Encapsulation (class) -- Information Hiding -- Interface and Implementations -- Standardization -- Access Control mechanisms (private /public etc.) Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Abstract Methods -- Abstract Classes

11/28/2015B.Ramamurthy4 Conventional Compiled Languages C++source Mac Compiler Mac Hardware PC Hardware Mac Compiler PC Compiler Sun Compiler Sun Hardware

11/28/2015B.Ramamurthy5 Java Virtual Machine Java source Mac Compiler Mac Hardware PC Hardware Mac interpreter PC Interpreter Sun Interpreter Sun Hardware JVM Byte code Java compiler :javac JVM

11/28/2015B.Ramamurthy6 “Run-anywhere” Capability On any machine when you compile Java source code using javac byte code equivalent to the source is generated. Byte code is machine-independent. This enables the “run-anywhere” capability. You invoke java command which will feed the byte code to the machine- dependent interpreter.

11/28/2015B.Ramamurthy7 Java Application Program Interface (Java API) (JAVA API) Package of related classes : java.awt java.util java.io, java.beans,.. Etc.. Random DateDictionary package class

11/28/2015B.Ramamurthy8 Java API : A Simplistic View API classes packages methods and data declarations

11/28/2015B.Ramamurthy9 Java API Classes Unlike many other languages, you will referring to the classes in the API. Where is the API? Application program interface

11/28/2015B.Ramamurthy10 Types of Programs Java is fully object-oriented. Every “function” has to be attached to a class. You will mainly deal with three types of programs: class: methods and data (variables + constants) describing a collection (type) of object application: class that has a main method: represents a our regular program applet: class that is meant for execution using a appletviewer/browser

11/28/2015B.Ramamurthy11 Problem Solving Using Java OO Design and Progamming in Java Identify classes needed Reuse API classes Reuse your classes Design new classes Write an application class Write an applet class Create and use objects

11/28/2015B.Ramamurthy12 What is an Object? Object-oriented programming supports the view that programs are composed of objects that interact with one another. How would you describe an object? Using its characteristics (has a ----?) and its behaviors (can do -- --?) Object must have unique identity (name) : Basketball, Blue ball Consider a ball: Color and diameter are characteristics (Data Declarations) throw, bounce, roll are behaviors (Methods)

11/28/2015B.Ramamurthy13 Classes are Blueprints A class defines the general nature of a collection of objects of the same type. The process creating an object from a class is called instantiation. Every object is an instance of a particular class. There can be many instances of objects from the same class possible with different values for data.

11/28/2015B.Ramamurthy14 Example class Rose blueRose redRose class objects Object References

11/28/2015B.Ramamurthy15 Instantiation : Examples class FordCar ---- defines a class name FordCar FordCar windstar; ---- defines a Object reference windStar windstar = new FordCar(); ---- instantiates a windstar Object class HousePlan1 { color…. HousePlan1 blueHouse; blueHouse = new HousePlan1(BLUE); HousePlan1 greenHouse = new HousePlan1(GREEN);

11/28/2015B.Ramamurthy16 Operator new and “dot” new operator creates a object and returns a reference to that object. After an object has been instantiated, you can use dot operator to access its methods and data declarations (if you have access permissions). EX: redRose.bloom(); greenHouse.color

11/28/2015B.Ramamurthy17 Elements of a Class class header methods data declarations (variables, constants) header body variables, constants statements modifiers, type, name parameters selection repetition assignment others

11/28/2015B.Ramamurthy18 Class Structure class variables constants methods

11/28/2015B.Ramamurthy19 Defining Classes Syntax: class class_name { data-declarations constructors methods } Constructors are special methods used for instantiating (or creating) objects from a class. Data declarations are implemented using variable and constant declarations.

11/28/2015B.Ramamurthy20 Naming Convention Constants: All characters in uppercase, words in the identifier separated by underscore: EX: MAX_NUM Variables, objects, methods: First word all lowercase, subsequent words start with uppercase. EX: nextInt, myPen, readInt() Instance variable begin with an _ Classes: Begin with an uppercase letter. EX: Tree, Car, System, Math

11/28/2015B.Ramamurthy21 Debugging and Testing Compile-time Errors : Usually typos or syntax errors Run-time Errors : Occurs during execution. Example: divide by zero. Logic Errors: Software will compile and execute with no problem, but will not produce expected results. (Solution: testing, and debugging)

11/28/2015B.Ramamurthy22 Class Components Class name (starts with uppercase), constants, instance variables, constructors definitions and method definitions. Constants: public final static double PI = 3.14; Variables: private double _bonus; public string _name;

11/28/2015B.Ramamurthy23 Operations Behaviors, methods or messages to which an object will respond to. Methods: 1. Constructors 2. Set/get methods (mutators/accesors) 3. Predicate methods (boolean/status indicators; Ex: isEmpty()) 4. Utility methods (for local use only: ex: internal sort after an insert) 5. Explicit methods or interface methods that define the interface an object offers to the world.

11/28/2015B.Ramamurthy24 Method Invocation/Call Syntax: method_name (values); object_name.method_name(values); classname.method_name(values); Examples: computeSum(); // call to method from within the class where it is located YourRose.paintIt(Red); Math.abs(X);

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

11/28/2015B.Ramamurthy26 Method Definition : Syntax visibility return_type method_name (parameter_list) { statements }

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

11/28/2015B.Ramamurthy28 Return Statement Syntax of return statement: return; // for void methods return expression; // for type or class return value // the expression type and return type should be same

11/28/2015B.Ramamurthy29 Parameter List Parameter list specified in method header provides a mechanism for sending information to a method. It is powerful mechanism for specializing an object. The parameter list that appears in the header of a method specifies the type and name of each parameter and is called formal parameter list. The corresponding parameter list in the method invocation is called an actual parameter list.

11/28/2015B.Ramamurthy30 Parameter list : Syntax Formal parameter list: This is like molds or templates (parm_type parm_name, parm_type parm_name,....) Actual parameter list: This is like material that fit into the mold or template specified in the formal list: (expression, expression....)

11/28/2015B.Ramamurthy31 Method Definition : review return typeName parameter list { statements } header body definition Visibility modifiers

11/28/2015B.Ramamurthy32 Method Definition : Example Write a method that computes and returns the perimeter of a rectangle class. Analysis: Send to the method: Length and Width Compute inside the method: Perimeter Return from the method: Perimeter

11/28/2015B.Ramamurthy33...Example (contd.) public int perimeter (int length, int width) { int temp; // local temporary variable temp = 2 * (length + width); // compute perimeter return temp; // return computed value }

11/28/2015B.Ramamurthy34 What happens when a method is called? Control is transferred to the method called and execution continues inside the method. Control is transferred back to the caller when a return statement is executed inside the method.

11/28/2015B.Ramamurthy35 Method Invocation : semantics 8 Main method Operating System Rect.area(….) area method 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

11/28/2015B.Ramamurthy36 Constructors A Constructor is used to create or instantiate an object from the class. Constructor is a special method: It has the same name as the class. It has no return type or return statement. Typically a class has more than one constructor: a default constructor which has no parameters, and other constructors with parameters. (overloading)

11/28/2015B.Ramamurthy37 Constructors (contd.) You don’t have to define a constructor if you need only a default constructor. When you want initializing constructors : 1. you must include a default constructor in this case. 2. You will use initializing constructors when you want the object to start with a specific initial state rather than as default state. 3. Example: Car myCar = new Car(RED); // initializing constructor for Car class with color as parameter

11/28/2015B.Ramamurthy38 Visibility Modifiers Method/variable name publicprotected “nothing” DEFAULT Package Visibility private type static “nothing” DEFAULT for class methods and variables for object methods and variables

11/28/2015B.Ramamurthy39..Modifiers (contd.) private : available only within class “nothing” specified : DEFAULT: within class and within package protected : within inherited hierarchy (only to sub classes) public : available to any class.

11/28/2015B.Ramamurthy40 Arrays Array is a numbered collection of variables all of the same type. Length attribute gives the capacity of the array Cells or the individual elements of the array distinguished by an index. Lets look at an example. Common error: ArrayIndexOutofBounds

11/28/2015B.Ramamurthy41 Summary An overview of OOP, problem solving using OOP and Java language was presented. Apply these concepts in your web services design