Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.

Similar presentations


Presentation on theme: "Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program."— Presentation transcript:

1 Object-Oriented Programming

2 An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.

3 Object-Oriented Programming An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program. Numbers

4 Object-Oriented Programming An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program. Text

5 Object-Oriented Programming An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program. Pictures

6 Object-Oriented Programming An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program. Sound

7 Object-Oriented Programming An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program. Video

8 Object-Oriented Programming An object is anything that can be represented by data.

9 Object-Oriented Programming An object can be something in the physical world or even just an abstract idea. An airplane, for example, is a physical object that can be manipulated by a computer.

10 Object-Oriented Programming An object can be something in the physical world or even just an abstract idea. A bank transaction is an example of an object that is not physical.

11 Object-Oriented Programming To a computer, an object is simply something that can be represented by data in the computer’s memory and manipulated by computer programs.

12 Object-Oriented Programming The data that represent the object are organized into a set of properties. The values stored in an object’s properties at any one time form the state of an object. Name: PA 3794 Owner:US Airlines Location:39 52′ 06″ N 75 13′ 52″ W Heading:271° Altitude:19 m AirSpeed:0 Make:Boeing Model:737 Weight:32,820 kg

13 Object-Oriented Programming Computer programs implement algorithms that manipulate the data. In object-oriented programming, the programs that manipulate the properties of an object are the object’s methods.

14 Object-Oriented Programming We can think of an object as a collection of properties and the methods that are used to manipulate those properties. Properties Methods

15 Object-Oriented Programming A class is a group of objects with the same properties and the same methods.

16 Object-Oriented Programming Each copy of an object from a particular class is called an instance of the object.

17 Object-Oriented Programming The act of creating a new instance of an object is called instantiation.

18 Object-Oriented Programming A class can be thought of as a blueprint for instances of an object.

19 Object-Oriented Programming Two different instances of the same class will have the same properties, but different values stored in those properties.

20 Object-Oriented Programming The same terminology is used in most object- oriented programming languages. Object Instance Property Method Instantiation Class State

21 21 Class Abstraction and Encapsulation Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.

22 22 Summary What is Object Oriented Programming? Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships

23 Java’s Inheritance Mechanism Class inheritance is the mechanism whereby a class acquires (inherits) the methods and variables of its superclasses. Rule: Subclasses inherit all public and protected variables and methods (except constructor methods). Classes become more specific as you move down the hierarchy. Subclass Superclass

24 Using an Inherited Method All classes are subclasses of the Object class. Any subclass of Object inherits the toString() method, meaning it can use it as its own. Student stu = new Student(“Stewart”); System.out.println(stu.toString()); // Prints Student@cde100 Invoking the inherited toString() method. public class Student { protected String name; public Student(String s) { name = s; } public String getName() { return name; }

25 Overriding an Inherited Method To override an inherited method means to give it a new definition in the subclass. Overriding a method allows you to customize its behavior in the subclass. Student stu = new Student(“Stu”); System.out.println(stu.toString()); // My name is Stu and I am a Student Invoking the overridden toString() method. public String toString() { return “My name is “ + name + “and I am a Student.”); }

26 Dynamic Binding and Polymorphism In dynamic binding (also called late or runtime binding) a method call is bound to the correct implementation of the method at runtime by the Java Virtual Machine. In static binding (for final and private methods) the Java compiler binds the method call to the correct method definition. Polymorphism (poly = many, morph = shape) is when the same method call can lead to different behaviors depending on the type of object on which the method is invoked. Object obj; // Static (declared) type: Object obj = new Student(“Stu”); // Actual (dynamic) type: Student System.out.println(obj.toString()); // Print “My name is Stu…” obj = new OneRowNim(11); // Actual (dynamic) type: OneRowNim System.out.println(obj.toString()); // Prints “nStidcks = 11,player = 1” The toString() method is polymorphic.

27 Polymorphic Methods A polymorphic method behaves differently when called on different objects. Consider the following method definition. public void polyMethod(Object obj) { System.out.println(obj.toString()); // Polymorphic } Student stu = new Student(“Stu”); polyMethod(stu); OneRowNim nim = new OneRowNim(); polyMethod(nim); In the following code segment, the first time polyMethod() is called, Java uses dynamic binding to associate toString() with the Student method. On the second call, it is associated with the OneRowNim toString() method.

28 Inheritance and Constructors Constructor methods are not inherited by subclasses. A subclass constructor can explicitly invoke a superclass method, including a superclass constructor, by using the super keyword. public class CollegeStudent extends Student { public CollegeStudent() { } public CollegeStudent(String s) { super(s); } public String toString() { return "My name is " + name + " and I am a CollegeStudent."; } In this context super() calls the superconstructor of the same signature to set the CollegeStudent’s inherited name variable.

29 Java Hierarchy The classes we have been using throughout the whole text book are organized into a hierarchy Object is the common ancestor –every class inherits characteristics of this class –for example: clone(), equals(), getClass() and toString() Every class must fit within the Java class hierarchy

30 Object-Orient Design (OOD) Identify the problem's objects –if an object cannot be represented by an existing type, design a class to do so –if two or more classes share common attributes, design a hierarchy Identify the operations If an operation cannot be performed with an existing operator or method –define a method to do so –store the method within a class hierarchy to enable inheritance Organize the objects and operations into an algorithm

31 O-O Design Issues Using the extends relationship: A class B should extend another class A if and only if B "is a" specialized version of A and … All messages that can be sent to A can be appropriately sent to B A B

32 Example: How we design ? Consider a collection of birds which have different properties –name –color (some of the same name are of different colors) –they eat different things –they make different noises –some make multiple kinds of sounds We seek a program to simulate this collection

33 Design Sketch Key is to design a Bird class hierarchy. Strategy –design classes for objects –identify characteristics classes have in common –design superclasses to store common characteristics

34 Hierarchy Bird call : ? color :? food :? movement :? WalkingBird call : ? color :? food :? movement : walked FlyingBird call : ? color :? food :? movement : flew Goose call : honk color : gray food : bugs Ostrich call : neek-neek color : brown food : grass Parrot call : Squawk color :? food : fruit Owl call :? color :? food : mice TalkingParrot...

35 Summary Of Important Points Inheritance is an object-oriented mechanism whereby subclasses inherit the public and protected instance variables and methods from their superclasses. Dyamic binding (or late binding) is the mechanism that associates a method call with the correct implementation of the method at run time. Static binding associates the method call with the method implementation at compile time. Polymorphism refers to the fact that a method call can result in different behaviors depeding on object on which it is invoked.

36 36


Download ppt "Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program."

Similar presentations


Ads by Google