Object-Oriented Programming: Inheritance and Polymorphism, Essays (high school) of Computer science

An introduction to inheritance and polymorphism in object-oriented programming. It explains how objects are grouped into categories and hierarchies, and how inheritance allows new classes to be created by specifying the differences between the new class and a pre-existing more general class. Polymorphism is discussed as the capability of objects to respond differently to the same method call based on their specific class. The document also touches upon the concepts of aggregation, behavior, messages, and the relationship between object-oriented programming and abstract data types.

Typology: Essays (high school)

2020/2021

Uploaded on 03/03/2022

unknown user
unknown user 🇬🇧

8 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
WEEK 3 INHERITANCE AND
POLYMORPHISM
INHERITANCE
We live in a world that can be considered to be comprised of countless objects, but how
do we think about objects? Do we treat each one on an individual basis? No, we tend to
group objects into categories and in turn these form a hierarchy.
For example consider a book. Lots of objects can be grouped together and called book
objects. Computer books, school books etc are all different types of books which can all
be described as books. A book is also part of a more general group called
publications, 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 a
cover, chapters, no advertising, and so on. They also have attributes shared by
publications in general: a title, a date of publication, a publisher, etc. They have
attributes 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 of
shared attributes using inheritance
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Object-Oriented Programming: Inheritance and Polymorphism and more Essays (high school) Computer science in PDF only on Docsity!

WEEK 3 – INHERITANCE AND

POLYMORPHISM

INHERITANCE

We live in a world that can be considered to be comprised of countless objects, but how do we think about objects? Do we treat each one on an individual basis? No, we tend to group objects into categories and in turn these form a hierarchy. For example consider a book. Lots of objects can be grouped together and called book objects. Computer books, school books etc are all different types of books which can all be described as books. A book is also part of a more general group called publications, 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 a cover, chapters, no advertising, and so on. They also have attributes shared by publications in general: a title, a date of publication, a publisher, etc. They have attributes 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 of shared attributes using inheritance

INHERITANCE

Object hierarchies arrange objects in such a way that general objects are at the top and specialised objects are at the bottom. Considering objects in this way it is soon noticed that objects share properties and methods. Inheritance supports this hierarchical-shared view by allowing new classes (of objects) to be created simply by specifying the differences between the new class and a pre-existing more general class. In object-speak the new specialised class, termed the descendant class, is said to be inherited from the pre-existing more general class termed the ancestor class. In simple terms inheritance means that descendant classes automatically get the properties and methods of the ancestor class. The primary advantage of inheritance is that we can reuse the properties and methods of pre-existing classes. Reuse is an important concept in software engineering because tried and tested ancestor classes provide a solid foundation for future projects which can proceed rapidly and with confidence. Another powerful advantage of inheritance is that modifications applied to methods in ancestor classes are automatically applied to all inheriting classes. This greatly simplifies software maintenance.

INHERITANCE

Inheritance specifies an “is-a-kind-of” relationshipMultiple classes share the same attributes and operations, allowing efficient code reuseExamples: ⚫ A customer “is a kind of” person ⚫ An employee “is a kind of” person Customer Employee Person Base Class Derived Classes

INHERITANCE(HIERARCHY)

Expresses commonality among objects

Allows code reusability

Highlights Generalization/Specialization relationships

We will learn more in the following lectures.

WEEK 5 – POLYMORPHISM

POLYMORPHISM

The ability of objects to respond differently to the

same message or function call.

We will discuss more in the following lectures.

POLYMORPHISM

Polymorphism refers to the capability of two or more objects belonging to

different classes to respond to exactly the same method call in different class-

specific 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 to

different professional classes. Each was given the same message—“cut”—but

knew the specific details of what this message meant to him or her by virtue of

knowing the profession (class) that he or she is associated with.

BASIC TERMINOLOGY: AGGREGATION

Aggregation describes a “has a” relationship. One object is a part of

another object.

We distinguish between composite aggregation (the composite “owns”

the part) and shared aggregation (the part is shared by more then

one composite).

13 A car has wheels.

BASIC TERMINOLOGY: BEHAVIOUR AND

MESSAGES

The most important aspect of an object is its behaviour (the things it

can do). A behaviour is initiated by sending a message to the object

(usually by calling a method).

14

C#, C++ AND JAVA

C# 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 object

oriented programming.

So, C++ is more powerful but also more difficult to handle than Java/C#.

OBJECT-ORIENTED PROGRAMMING

Object is derived from abstract data type

Object-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.

OBJECT-ORIENTED PROGRAMMING Each object is responsible to initialize and destroy itself correctly. Consequently, there is no longer the need to explicitly call a creation or termination procedure. KAY’S DESCRIPTION OF OOP Everything is an object Objects perform computation by making requests of each other through the passing of messages Every 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 object Classes are organized into singly-rooted tree structure, called an inheritance hierarchy.

ADT AND OBJECT-ORIENTATION

ADTs allows the creation of instances with well-defined

properties and behavior.

In object-orientation, ADTs are referred to as classes.

Therefore, a class defines properties of objects which are

the instances in an object-oriented environment.

ADT AND OBJECT-ORIENTATION(2) ADTs define functionality by putting main emphasis on the involved data, their structure, operations as well as axioms and preconditions. Object-oriented programming is ``programming with ADTs'': combining functionality of different ADTs to solve a problem. Therefore instances (objects) of ADTs (classes) are dynamically created, destroyed and used.

OBJECT-ORIENTATION EVOLUTION

Modules

Information hiding

Data encapsulation

Abstract data types

Classes and Objects

SIMPLY REMEMBER

Encapsulation(Data & Operations)--- A technique for Information

Hiding-----the users of the objects could not see the details of the

data 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 THE LARGE Programming in the Small:  One programmer, understands everything from top to bottom.  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 and communication between programmers and between their respective 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 when using a non-OO language. ▪ Divides code into logical chunks individuals of a team can be experts in, and outsiders 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 stays the same