Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply



Published on

Published in: Technology

1 Comment
  • i cant download this plz give me download
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Overview   Understand Classes and Objects. Introduction to Object Oriented Design   Understand some of the key concepts/ features in the Object Oriented paradigm.   Benefits of Object Oriented Design paradigm. 1 2 OOP: model, map, reuse, extend Examples of Objects ¡ Model the real world problem to user’s perceive; ¡ Use similar metaphor CAR BOY GIRL CLOCK in computational env. ¡ Construct reusable components; ¡ Create new components from VDU BOOK TREE TRIANGLE existing ones. Figure 1.9: Examples of objects 3 4 Classes: Objects with the same Object Oriented Paradigm: Features attributes and behavior Person Objects Encapsulation Abstract Person Class Data Abstraction Into Attributes: Name, Age, Sex Operations: Speak(), Listen(), Walk() Single Inheritance Vehicle Objects Polymorphism OOP Vehicle Class Abstract Attributes: Name, Model, Color Paradigm Into Operations: Start(), Stop(), Accelerate() Persistence Polygon Objects Delegation Polygon Class Abstract Attributes: Vertices, Border, Into Color, FillColor Genericity Operations: Draw(), Erase(), Move() Multiple Inheritance Figure 1.12: Objects and classes 5 6
  • 2. Java’s OO Features Encapsulation Encapsulation Encapsulation ¡ It associates the code Data Abstraction Data Abstraction and the data it manipulates into a Single Inheritance Single Inheritance single unit; and OOP Polymorphism OOP Polymorphism keeps them safe from Java Paradigm Paradigm external interference Persistence Persistence and misuse. Delegation Delegation Genericity Genericity Data Functions Multiple Inheritance Multiple Inheritance 7 8 Data Abstraction Abstract Data Type (ADT) Encapsulation The technique of A structure that contains both data ¢ £ Data Abstraction creating new data types that are well suited to an and the actions to be performed on application. Single Inheritance that data. ¢ It allows the creation of Polymorphism OOP user defined data types, Paradigm having the properties of Persistence built data types and a set £ Class is an implementation of an of permitted operators. Abstract Data Type. Delegation ¢ In Java, partial support. Genericity ¢ In C+ + , fully supported (e.g., operator Multiple Inheritance overloading). 9 10 Class- Example Class class Account { ¤ Class is a set of attributes and operations private String accountName; that are performed on the attributes. private double accountBalance; Student Circle public withdraw(); Account public deposit(); accountName accountBalance name age centre radius studentId public determineBalance(); withdraw() area() deposit() getName() } / / Class Account determineBalance() getId() circumference() 11 12
  • 3. Objects Classes/ Objects John and Jill are £ An Object Oriented system is a :John objects of class Student collection of interacting Objects. Student :Jill £ Object is an instance of a class. :circleA circleA and circleB Circle are :circleB objects of class Circle 13 14 Class Object ¤ A class represents a template for several ¤ Objects have state and classes don’t. John is an object (instance) of class Student. objects that have common properties. name = “John”, age = 20, studentI d = 1236 Jill is an object (instance) of class Student. ¤ A class defines all the properties common name = “Jill”, age = 22, studentId = 2345 to the object - attributes and methods. circleA is an object (instance) of class Circle. centre = (20,10), radius = 25 ¤ A class is sometimes called the object’s circleB is an object (instance) of class Circle. centre = (0,0), radius = 10 type. 15 16 Encapsulation Encapsulation - Example ¥ All information (attributes and methods) in an class Account { object oriented system are stored within the private String accountName; message object/ class. private double accountBalance; ¥ Information can be manipulated through Withdraw Deposit operations performed on the object/ class – public withdraw(); public deposit(); messag Account interface to the class. Implementation is hidden e balance from the user. public determineBalance(); ¥ Object support Information Hiding – Some } / / Class Account Determine Balance attributes and methods can be hidden from the user. message 17 18
  • 4. Data Abstraction Abstraction - Example ¤ The technique of creating new data types class Account { that are well suited to an application. private String accountName; Creates a data private double accountBalance; type Account public withdraw(); ¤ I t allows the creation of user defined data public deposit(); Account acctX; types, having the properties of built in public determineBalance(); } // Class Account data types and more. 19 20 Inheritance Inheritance - Example ¤ New data types (classes) can be defined ¥ Example ¦ Define Person to be a class as extensions to previously defined types. § A Person has attributes, such as age, height, gender Assign values to attributes when describing object Parent Class (Super Class) – Child Class § ¤ (Sub Class) ¦ Define student to be a subclass of Person Parent § A student has all attributes of Person, plus attributes of ¤ Subclass inherits his/her own ( student no, course_enrolled) A student has all attributes of Person, plus attributes of properties from the § Inherited his/her own (student no, course_enrolled) capability A student inherits all attributes of Person parent class. § ¦ Define lecturer to be a subclass of Person § Lecturer has all attributes of Person, plus attributes of Child his/her own ( staff_id, subjectI D1, subjectID2) 21 22 Inheritance - Example Inheritance - Example ¤ Circle Class can be a subclass (inherited ¤ I nheritance can also have multiple levels. from ) of a parent class - Shape Shape Shape Circle Rectangle Circle Rectangle GraphicCircle 23 24
  • 5. Uses of I nheritance - Reuse Uses of I nheritance - Reuse Circle Rectangle ¥ If multiple classes have common centre attributes/ methods, these methods can be centre height radius width moved to a common class - parent class. area() area() circumference() circumference() move(newCentre) move(newCentre) ¥ This allows reuse since the implementation is not repeated. move(newCentre){ centre = newCentre; move(newCentre){ Example : Rectangle and Circle method have a } centre = newCentre; } common method move(), which requires changing the centre coordinate. 25 26 Uses of I nheritance - Reuse Uses of I nheritance - Specialization Shape ¤ Specialized behavior can be added to the centre move(newCentre){ centre = newCentre child class. area() } circumference() move(newCentre) ¤ I n this case the behaviour will be implemented in the child class. ¨ E.g. The implementation of area() method in Rectangle the Circle class is different from the Circle Rectangle class. height radius width ¤ area() method in the child classes area() area() override the method in parent classes(). circumference() circumference() 27 28 Uses of I nheritance - Specialization Uses of I nheritance - Specialization Shape Circle Rectangle centre area(); - Not implemented centre centre And left for the child classes height radius width area() To implement area() area() circumference() circumference() circumference() move(newCentre) move(newCentre) move(newCentre) area(){ area(){ Circle Rectangle return pi* r^ 2; return pi* r^ 2; area(){ } } return height* width; height } radius width area() area(){ area() circumference() return height* width; circumference() } 29 30
  • 6. Uses of I nheritance – Common Interface Uses of I nheritance - Extension ¥ All the operations that are supported for ¤ Extend functionality of a class. Rectangle and Circle are the same. ¥ Some methods have common implementation ¤ Child class adds new operations to the and others don’t. parent class but does not change the ¦ move() operation is common to classes and can be implemented in parent. inherited behavior. ¦ circumference(), area() operations are significantly different and have to be implemented in the respective classes. ¨ E.g. Rectangle class might have a special ¥ The Shape class provides a common interface operation that may not be meaningful to the where all 3 operations move(), circumference() Circle class - rotate90degrees() and area(). 31 32 Uses of I nheritance - Extension Uses of I nheritance – Multiple I nheritance Shape ¤ I nherit properties from more than one centre area() class. circumference() move(newCentre) ¤ This is called Multiple Inheritance. Graphics Shape Circle Rectangle height radius width area() area() circumference() circumference() Circle rotate90degrees() 33 34 Uses of Multiple Inheritance Uses of I nheritance – Multiple I nheritance GraphicCircle Shape ¤ This is required when a class has to centre inherit behavior from multiple classes. color area() paint() circumference() ¤ I n the example Circle class can inherit move(newCentre) move() operation from the Shape class and the paint() operation from the Graphics class. Circle ¤ Multiple inheritance is not supported in JAVA but is supported in C+ + . radius area() circumference() 35 36
  • 7. Polymorphism Polymorphism ¥ Polymorphic which means “many forms” has ¥ An object of type Circle or Rectangle can be Greek roots. assigned to a Shape object. The behavior of the ¦ Poly – many object will depend on the object passed. ¦ Morphos - forms. circleA = new Circle(); Create a new circle object ¥ In OO paradigm polymorphism has many Shape shape = circleA; forms. shape.area(); area() method for circle class will be executed ¥ Allow a single object, method, operator rectangleA = new Rectangle(); Create a new rectangle object associated with different meaning depending shape= rectangle; on the type of data passed to it. shape.area() area() method for rectangle will be executed. 37 38 Polymorphism – Method Overloading Polymorphism – Operator Overloading ¤ Multiple methods can be defined with the ¤ Allows regular operators such as + , -, * , / same name, different input arguments. to have different meanings based on the Method 1 - initialize(int a) type. Method 2 - initialize(int a, int b) ¤ E.g. + operator for Circle can re-defined ¤ Appropriate method will be called based Circle c = c + 2; on the input arguments. initialize(2) Method 1 will be called. ¤ Not supported in JAVA. C+ + supports it. initialize(2,4) Method 2 will be called. 39 40 Persistence Why OOP? ¤ The phenomenon where the object ¤ Greater Reliability outlives the program execution. ¨ Break complex software projects into small, self-contained, and modular objects ¤ Databases support this feature. ¤ Maintainability ¨ Modular objects make locating bugs easier, with less impact on the overall project ¤ I n Java, this can be supported if users explicitly build object persistency using I O ¤ Greater Productivity through Reuse! streams. ¤ Faster Design and Modelling 41 42
  • 8. Benefits of OOP.. Benefits of OOP.. ¤ I nheritance: Elimination of Redundant ¤ Multiple objects to coexist without any Code and extend the use of existing interference. classes. ¤ Easy to map objects in problem domain ¤ Build programs from existing working to those objects in the program. modules, rather than having to start from ¤ I t is easy to partition the work in a scratch. Æ save development time and project based on objects. get higher productivity. ¤ The Data-Centered Design enables us in ¤ Encapsulation: Helps in building secure capturing more details of model in an programs. implementable form. 43 44 Benefits of OOP.. Summary ¤ Object Oriented Systems can be easily © Object Oriented Design, Analysis, and Programming is a Powerful paradigm upgraded from small to large systems. © Enables Easy Mapping of Real world Objects to Objects ¤ Message-Passing technique for in the Program © This is enabled by OO features: communication between objects make Encapsulation the interface descriptions with external Data Abstraction Inheritance systems much simpler. Polymorphism ¤ Software complexity can be easily Persistence © Standard OO Design (UML) and Programming managed. Languages (C+ + / Java) are readily accessible. 45 46 Reference ¤ Chapter 1: “Programming with Java” by Balagurusamny ¤ Optional: ¨ Chapter 1: “Mastering C+ + ” by V. Rajuk and R. Buyya, Tata McGraw Hill, New Delhi, India. 47