Introducing Object-Oriented Programming (OOP). Goals. By the end of this lecture, you should … Understand the three pillars of Object-Oriented Programming: Inheritance, Encapsulation and Polymorphism. Understand what an object is. Understand object attributes, methods and events. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• By the end of this lecture, you should …By the end of this lecture, you should …• Understand the three pillars of Object-Understand the three pillars of Object-
Oriented Programming: Inheritance, Oriented Programming: Inheritance, Encapsulation and Polymorphism.Encapsulation and Polymorphism.
• Understand what an object is. Understand what an object is. • Understand object attributes, methods Understand object attributes, methods
and events.and events.• Understand how programmers use APIs.Understand how programmers use APIs.
• Programming languages allow Programming languages allow programmers to code software.programmers to code software.
• The three major families of The three major families of languages are:languages are:– Machine languagesMachine languages– Assembly languagesAssembly languages– High-Level languagesHigh-Level languages
• Comprised of 1s and 0sComprised of 1s and 0s• The “native” language of a computerThe “native” language of a computer• Difficult to program – one misplaced Difficult to program – one misplaced
1 or 0 will cause the program to fail.1 or 0 will cause the program to fail.• Example of code:Example of code:1110100010101 111010101110 1110100010101 111010101110 10111010110100 1010001111011110111010110100 10100011110111
• Assembly languages are a step towards Assembly languages are a step towards easier programming. easier programming.
• Assembly languages are comprised of a Assembly languages are comprised of a set of elemental commands which are tied set of elemental commands which are tied to a specific processor.to a specific processor.
• Assembly language code needs to be Assembly language code needs to be translated to machine language before the translated to machine language before the computer processes it.computer processes it.
• High-level languages represent a giant High-level languages represent a giant leap towards easier programming.leap towards easier programming.
• The syntax of HL languages is similar The syntax of HL languages is similar to English. to English.
• Historically, we divide HL languages Historically, we divide HL languages into two groups:into two groups:– Procedural languagesProcedural languages– Object-Oriented languages (OOP)Object-Oriented languages (OOP)
• Early high-level languages are Early high-level languages are typically called procedural languages.typically called procedural languages.
• Procedural languages are Procedural languages are characterized by sequential sets of characterized by sequential sets of linear commands. The focus of such linear commands. The focus of such languages is on languages is on structurestructure..
• Examples include C, COBOL, Fortran, Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScriptLISP, Perl, HTML, VBScript
• ObjectObject – Unique programming entity – Unique programming entity that has that has methodsmethods, has , has attributes attributes and and can react to can react to eventsevents..
• MethodMethod – Things which an object – Things which an object can do; the “verbs” of objects. In can do; the “verbs” of objects. In code, usually can be identified by an code, usually can be identified by an “action” word -- “action” word -- HideHide,, Show Show
• AttributeAttribute – Things which describe an – Things which describe an object; the “adjectives” of objects. In object; the “adjectives” of objects. In code, usually can be identified by a code, usually can be identified by a “descriptive” word – “descriptive” word – EnabledEnabled, , BackColorBackColor
• EventsEvents – Forces external to an object – Forces external to an object to which that object can react. In code, to which that object can react. In code, usually attached to an event procedureusually attached to an event procedure
• ClassClass – Provides a way to create new – Provides a way to create new objects based on a “meta-definition” objects based on a “meta-definition” of an object (Example: The of an object (Example: The automobile automobile classclass))
• ConstructorsConstructors – Special methods – Special methods used to create new instances of a used to create new instances of a class (Example: A Honda Civic is an class (Example: A Honda Civic is an instanceinstance of the automobile of the automobile classclass.).)
• Allows programmers to create new Allows programmers to create new classes based on an existing classclasses based on an existing class
• Methods and attributes from the Methods and attributes from the parent class are inherited by the parent class are inherited by the newly-created classnewly-created class
• New methods and attributes can be New methods and attributes can be created in the new class, but don’t created in the new class, but don’t affect the parent class’s definitionaffect the parent class’s definition
• Creating methods which describe Creating methods which describe the way to do some general the way to do some general function (Example: The “drive” function (Example: The “drive” method in the automobile class)method in the automobile class)
• Polymorphic methods can adapt Polymorphic methods can adapt to specific types of objects.to specific types of objects.
• A A classclass is a data type that allows programmers is a data type that allows programmers to create objects. A class provides a definition to create objects. A class provides a definition for an object, describing an object’s attributes for an object, describing an object’s attributes (data) and methods (operations).(data) and methods (operations).
• An object is an An object is an instanceinstance of a class. With one of a class. With one class, you can have as many objects as required.class, you can have as many objects as required.
• This is analogous to a variable and a data type, This is analogous to a variable and a data type, the class is the data type and the object is the the class is the data type and the object is the variable.variable.
Sample Class DefintionSample Class DefintionClass CubeClass Cube
Side As RealSide As RealVolume As RealVolume As RealSubprogram SetSide(NewSide)Subprogram SetSide(NewSide)
Set Side = NewSideSet Side = NewSideEnd SubprogramEnd Subprogram
Subprogram ComputeVolume()Subprogram ComputeVolume()Set Volume = Side ^ 3Set Volume = Side ^ 3End SubprogramEnd Subprogram
Function GetVolume() As RealFunction GetVolume() As RealSet GetVolume = VolumeSet GetVolume = VolumeEnd FunctionEnd Function
Function GetSide() As RealFunction GetSide() As RealSet GetSide = SideSet GetSide = SideEnd FunctionEnd Function
End ClassEnd Class
The class Cube is similar The class Cube is similar to the definition of a to the definition of a record, but also has record, but also has functions added that are functions added that are part of the objects part of the objects created. So we can think created. So we can think of it as an object ‘has’ of it as an object ‘has’ data attributes and data attributes and function attributesfunction attributes
The class Cube is similar The class Cube is similar to the definition of a to the definition of a record, but also has record, but also has functions added that are functions added that are part of the objects part of the objects created. So we can think created. So we can think of it as an object ‘has’ of it as an object ‘has’ data attributes and data attributes and function attributesfunction attributes
Is JavaScript an OOP language?Is JavaScript an OOP language?
• Well, not really …Well, not really …• We call JavaScript an "object-We call JavaScript an "object-
inspired" languageinspired" language• It uses objects by way of supporting It uses objects by way of supporting
inheritance and encapsulation, but it inheritance and encapsulation, but it doesn't really provide support for doesn't really provide support for polymorphism.polymorphism.
• More like “Object-Inspired” JavaScriptMore like “Object-Inspired” JavaScript• We can create new, custom, re-usable We can create new, custom, re-usable
objects in JavaScript that include their objects in JavaScript that include their own methods, properties and events.own methods, properties and events.
• Consider the following problem: “I want Consider the following problem: “I want to record the color, brand, horsepower to record the color, brand, horsepower and price of several cars.”and price of several cars.”
• API stands for Application API stands for Application Programming InterfaceProgramming Interface
• Allows programmers to extend the Allows programmers to extend the current language to included current language to included customized componentscustomized components
• Most modern languages incorporate Most modern languages incorporate APIsAPIs