Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming The object concept
2
Alan Kay—Smalltalk—5 basic characteristics of OOP 1.Everything is an object. An object is a fancy variable-it stores data, but it is also asked to perform operations on itself by making requests. 2.A program is a bunch of objects telling each other what to do by sending messages. A message is a request to call a function that belongs to a particular object.
3
Alan Kay—Smalltalk—5 basic characteristics of OOP 3. Each object has its own memory made up of other objects. Also, a new kind of object can be created by making up a package containing existing objects. Complexity is built up while at the same time, objects have simplicity. 4. Every object has a type. In Java-speak, each object is an”instance of a class”, where class = type. Important distinguishing characteristic of a class: what messages can be sent to it?
4
Alan Kay—Smalltalk—5 basic characteristics of OOP 5. All objects of a particular type can receive the same messages. If an object of type circle is also an object of type shape, a circle is guaranteed to receive shape messages. Can write code to talk to shapes and automatically handle anything that fits the description of a shape. This “substitutability” is a powerful concept of OOP.
5
Six Ideas in Object Oriented Programming zClasses zInstances zEncapsulation z Overloading z Inheritance z Polymorphism
6
Six Ideas in Object Oriented Programming z(1) Classes: the fundamental structure of every Java program, containing data fields and mechanisms to manipulate that data; classes can provide blueprints to construct software objects z“class” is a keyword that precedes the class name zClass is a user-defined type, and instances of such a type are called objects.
7
Six Ideas in Object Oriented Programming (1-- Classes) zClasses consist of any number of two basic kinds of class members, called fields: variables and methods.
8
Six Ideas in Object Oriented Programming (1 -- Classes) zThe left brace immediately following the class name begins the definition of the class zA matching right brace is needed to end the class definition zex:class binary{ }
9
Six Ideas in Object Oriented Programming (1 -- Classes) zWrapper classes: wraps a single primitive value inside an object for structures that work with objects instead of primitive data types (ex:Vectors, which will be discussed later) or for static methods related to the corresponding primitive data type.
10
Six Ideas in Object Oriented Programming (1 -- Classes) zAll wrapper classes have names that are closely related to their corresponding basic types, but staring with a capital letter zsyntax: WrapperName varName [=new WrapperName (basicType)]; zWrapperName=Character,Double or Integer zbasicType=char,double or int
11
Six Ideas in Object Oriented Programming z[2] Instances: objects that were constructed according to the class blueprints and that exist in the memory of the computer. zOne class can be used to instantiate a number of objects.
12
Six Ideas in Object Oriented Programming [2-- Instance] zInstance emphasizes an object’s relationship to its class zex: suppose a Car class is used to create the objects hotrod, racer, and junker; all these objects have their fields defined in the Car declaration; hotrod, racer and junker are “instances of Car”.
13
Six Ideas in Object Oriented Programming zEncapsulation: allowing or disallowing access to the data and mechanisms that a class provides zIt is the ability to hide internal detail while providing a public interface to a user- defined type; Java uses the class declarations in conjunction with the access keywords “private” and “public” to provide encapsulation.
14
Six Ideas in Object Oriented Programming [3-- Encapsulation] zpublic-- a field, method or class that is accessible to every class. zprotected-- a field, method or class that is accessible to the class itself, subclasses, and all classes in the same package or directory.
15
Six Ideas in Object Oriented Programming [3-- Encapsulation] zfriendly-- a field, method or class accessible to the class itself and to all classes in the same package or directory. A field or method is declared friendly if other modifiers are absent. zprivate-- a field or method accessible only to the class in which it is defined. A class cannot be declared private as a whole. zEx--tester,tester2
16
Six Ideas in Object Oriented Programming z(4) Overloading--providing multiple definitions for certain mechanisms,allowing them to adjust to different situations. z“+” is overloaded--used for numeric addition as well as String concatenation. zEx--Overloading order (identical arguments for print method, but different order)
17
Six Ideas in Object Oriented Programming z(5)Inheritance--a mechanism to improve the relationships between classes and their corresponding objects. zClass or classes inherited from are called superclasses, or ancestors. zInheriting classes are called subclasses, or descendants zInheritance is indicated by keyword “extends”. Ex:Rings2
18
Six Ideas in Object Oriented Programming z(6)Polymorphism--The ability to deal with multiple related classes based on a common feature, based on a common feature, giving an entire class hierarchy the ability to adjust to an appropriate situation. zThe compiler selects at runtime those methods and objects from an appropriate inheritance hierarchy.
19
Six Ideas in Object Oriented Programming (6) zEx-ShapeTester: Circle,Square,Rectangle,Shape. zShape=superclass, zthree fields=name,area,perimeter ztwo methods=constructor and display method zCircle,Rectangle,Square=subclasses (descendants)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.