WEEK 3 –INHERITANCE ANDPOLYMORPHISMINHERITANCEWe live in a world that can be considered to be comprised of countless objects, but howdo we think about objects? Do we treat each one on an individual basis? No, we tend togroup objects into categories and in turn these form a hierarchy.For example consider a book. Lots of objects can be grouped together and called bookobjects. Computer books, school books etc are all different types of books which can allbe described as books. A book is also part of a more general group calledpublications, in addition to books, publications include scientific journals, newspapers,comics, magazines etc.Books have certain attributes or properties that are shared by all books: They have acover, chapters, no advertising, and so on. They also have attributes shared bypublications in general: a title, a date of publication, a publisher, etc. They haveattributes that are shared by all physical objects: a location, size, shape, and weight.This idea of shared attributes is very important in OOP. OOP models the concept ofshared attributes using inheritanceINHERITANCEObject hierarchies arrange objects in such a way that general objects are at the topand specialised objects are at the bottom. Considering objects in this way it is soonnoticed that objects share properties and methods. Inheritance supports thishierarchical-shared view by allowing new classes (of objects) to be created simply byspecifying the differences between the new class and a pre-existing more generalclass.In object-speak the new specialised class, termed the descendant class, is said to beinherited from the pre-existing more general class termed the ancestor class.In simple terms inheritance means that descendant classes automatically get theproperties and methods of the ancestor class.The primary advantage of inheritance is that we can reuse the properties andmethods of pre-existing classes. Reuse is an important concept in softwareengineering because tried and tested ancestor classes provide a solid foundation forfuture projects which can proceed rapidly and with confidence. Another powerfuladvantage of inheritance is that modifications applied to methods in ancestor classesare automatically applied to all inheriting classes. This greatly simplifies softwaremaintenance.INHERITANCE◼ Inheritance specifies an “is-a-kind-of” relationship◼ Multiple classes share the same attributes andoperations, allowing efficient code reuse◼ Examples:⚫ A customer “is a kind of” person⚫ An employee “is a kind of” person CustomerEmployee PersonBase ClassDerived ClassesCLASS DIAGRAM – INHERITANCEHOW TO INHERIT FROM A CLASSA derived class inherits from a base classProperties, methods, data members, events, and event handlers can beinherited (dependent on scope)Keywords (Convert this to C#) Inherits – inherits from a base class NotInheritable – cannot be inherited from MustInherit – instances of the class cannot be created; must be inherited from as abase classINHERITANCE(HIERARCHY)Expresses commonality among objectsAllows code reusabilityHighlights Generalization/Specialization relationshipsWe will learn more in the following lectures.WEEK 5 – POLYMORPHISMPOLYMORPHISMThe ability of objects to respond differently to thesame message or function call.We will discuss more in the following lectures.POLYMORPHISMPolymorphism refers to the capability of two or more objects belonging todifferent classes to respond to exactly the same method call in different classspecific ways. As an example, suppose that we instruct three different people—a surgeon, a hair stylist, and an actor—to “cut”: The surgeon would begin to make an incision. The hair stylist would begin to cut someone’s hair. The actor would abruptly stop acting out the current scene, awaiting directorial guidance.These three different professionals can be thought of as objects belonging todifferent professional classes. Each was given the same message—“cut”—butknew the specific details of what this message meant to him or her by virtue ofknowing the profession (class) that he or she is associated with.POLYMORPHISMPolymorphism means “having many forms”. It allows different objects torespond to the same message in different ways, the response specific tothe type of the object.11E.g. the message OpenWindow() of the Windows classshould give different results when send to msPPt object(e.g. Slide Show).POLYMORPHISMThe method name resides in the base classThe method implementations reside in the derived classes BaseTaxCalculateTax( ) CountyTaxCityTaxCalculateTax( )CalculateTax( ) BASIC TERMINOLOGY: AGGREGATIONAggregation describes a “has a” relationship. One object is a part ofanother object.We distinguish between composite aggregation (the composite “owns”the part) and shared aggregation (the part is shared by more thenone composite).13A car has wheels.BASIC TERMINOLOGY: BEHAVIOUR ANDMESSAGESThe most important aspect of an object is its behaviour (the things itcan do). A behaviour is initiated by sending a message to the object(usually by calling a method).14C#, C++ AND JAVAC# and Java are full object oriented language, all code has to go into classes.C++ – in contrast – is a hybrid language, capable both of functional and objectoriented programming.So, C++ is more powerful but also more difficult to handle than Java/C#.OBJECT-ORIENTED PROGRAMMINGObject is derived from abstract data typeObject-oriented programming has a web of interacting objects,each house-keeping its own state.Objects of a program interact by sending messages to each other.Object1Data1+Procedures1Data Data1 Object3Data3 + Procedures3Object2Data2 + Procedures2Object4Data4 + Procedures4INTERACTIONOBJECT-ORIENTED PROGRAMMINGIn object-oriented programming , instead of calling a procedurewhich we must provide with the correct handle, we would directlysend a message to the object in questions.Roughly speaking, each object implements its own module.OBJECT-ORIENTED PROGRAMMINGEach object is responsible to initialize anddestroy itself correctly.Consequently, there is no longer the needto explicitly call a creation or terminationprocedure.KAY’S DESCRIPTION OF OOPEverything is an objectObjects perform computation by making requests of each other through thepassing of messagesEvery object has it’s own memory, which consists of other objects.Every object is an instance of a class. A class groups similar objects.The class is the repository for behavior associated with an objectClasses are organized into singly-rooted tree structure, called an inheritancehierarchy.ADT AND OBJECT-ORIENTATIONADTs allows the creation of instances with well-definedproperties and behavior.In object-orientation, ADTs are referred to as classes.Therefore, a class defines properties of objects which arethe instances in an object-oriented environment.ADT AND OBJECT-ORIENTATION(2)ADTs define functionality by putting main emphasison the involved data, their structure, operations aswell as axioms and preconditions.Object-oriented programming is “programmingwith ADTs”: combining functionality of differentADTs to solve a problem.Therefore instances (objects) of ADTs (classes) aredynamically created, destroyed and used.•ADTs still lack self-reference and conceptual locality ofobjects.•Data types and operations are almost objects, butneed to put them together and give the result a namewhich can be self-referenced.ADT AND OBJECTORIENTATION(3)A FORMULAObject-Orientation =Object (Abstraction) +Class (ADT & Classification) +Inheritance (Reusing) +Dynamic Binding (Polymorphism,Flexibility)OBJECT-ORIENTATION EVOLUTIONModulesInformation hidingData encapsulationAbstract data typesClasses and ObjectsSIMPLY REMEMBEREncapsulation(Data & Operations)— A technique for InformationHiding—–the users of the objects could not see the details of thedata and operations of the objects.Data Abstraction —- the procedure to find a class from objects.Abstract Data Type—- Class.PROGRAMMING IN THE SMALL AND PROGRAMMING IN THELARGEProgramming in the Small: One programmer, understands everything from top tobottom. Major problem is the development of algorithms.Programming in the Large: System is developed by large team of programmers Major problems are management of details andcommunication between programmers and between theirrespective software subsystems.OBJECTS AND LARGE SOFTWARE SYSTEMS(2) Object view▪ Makes systems more intuitively understandable▪ Unifies design and programming method▪ Initial program thoughts are informal objects-and-interactions descriptions, even whenusing a non-OO language.▪ Divides code into logical chunks individuals of a team can be experts in, andoutsiders can understand via interfaces▪ Allows “off the shelf” code libraries to be reused▪ Supports code evolution: internals can always be re-written as long as interface staysthe sameOBJECTS AND LARGE SOFTWARE SYSTEMS(3)So, in large software system, we need more CONCEPTS and STRUCTURESof object-orientation than PROGRAMMING SKILLSThose are OOA(Object-Oriented Analysis) and OOD(Object-OrientedDesign), that will be discussed more deeply in Software engineering orSoftware designing methodology.


Leave a Reply

Your email address will not be published. Required fields are marked *