0% found this document useful (0 votes)
3 views

Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that utilizes objects created from classes to promote code reusability and organization. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which help in managing complex programs by grouping related data and functions together. While OOP offers advantages like modularity and maintainability, it may not be suitable for simpler programs and can lead to longer execution times.

Uploaded by

shaambavy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that utilizes objects created from classes to promote code reusability and organization. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which help in managing complex programs by grouping related data and functions together. While OOP offers advantages like modularity and maintainability, it may not be suitable for simpler programs and can lead to longer execution times.

Uploaded by

shaambavy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

WHAT IS OOP?

A popular paradigm that is applicable to certain problems with reusable


components with similar characteristics-it builds on entities called objects
formed from classes that have certain attributed and methods.

AN OBJECT is an instance of a class

A CLASS is a template for objects it defines the state and behaviour of an


object

-focuses on making reusable and easy to update programming

-previously primitive data types like strings, integers etc were used for
programming however as it got more complex this was not efficient -
programmers wanted to group similar pieces of data and functions
together to use throughout a program

-imagine you are programming a chess game and are focussing on the
knight piece there are many variables you would need eg position
variable, Boolean to show if it is captured ,variable to show colour .You
would need one of these variables for each piece

-in complex programs grouping variables together is essential

Classes can create objects through instantiation-a class can create


multiple objects wit same set of attributes and methods

Set reserved and set onlon are a special method called setters it can set
the value of a particular attribute of an object

A getter method is one that retrieves the value of a given attriute

ATTRIBUTES ARE PRIVATE they can not directly be accessed and edited by
the users (encapsulation) they can only be altered via public methods

A constructor method new allows a new object to be created


ENCAPSULATION
-refers to bundling data with methods that can operate on that data within
a class

-idea of hiding data within a class that anything outside that class cannot
directly interact with

-members of other classes can only interact with attributes of another


object through its METHODS(functions defined within the class) ,a
common example is getting and setting methods

-the setting method allows attributed to be changed rather than requiring


you to individually change them -this also allows validation to prevent
errors

-you can choose to only allow a getter method

ABSTRACTION
-only showing essential details(encapsulation hides internal details and
exposes only necessary functionality)

-methods inner workings can be hidden in some cases (eg in inheritance


you build upon

-allows the program to be split up between people to work in a group

INHERITANCE
-allows you to derive classes from other classes it inherits methods and
attributes of another class the sub class can also have its own additional
properties

-this is very efficient as you can extend a subclasses attributes and


methods without affecting original code

The super class is what sub classes inherit from

POLYMORPHISM
A property that means objects can behave differently depending on their
class -this can result in the SAME method producing DIFFERENT outputs
depending on the object involved

1. For example, if you have a makeSound() method in both a Dog class


and a Cat class, calling makeSound() on a Dog object would produce
a bark sound, while calling it on a Cat object would produce a meow
sound.
OVERRIDING-redefining a method within a subclass and altering it so it
functions differently to produce a different output for that specific
subclass-

the subclass provides a specific implementation of a method that is


already defined in its superclass that is used overriding the old one for
that specific subclass

also known as DYNAMIC polymorphism

Eg if we wanted a method to be more specific and output something extra


eg initials weuse the method with the same name BUT when called the
method from the parent class is overridden

Basically method with the same name and parameters but providing
different functionality depending on if its in the sub or super class

OVERLOADING- passing different parameters into a method

-also known as STATIC polymorphism it allows you to implement multiple


methods of the same name with different parameters in the same class

The difference in parameters can be a different number of them, the types


(eg string vs integer) OR a different order of acceptance

This concept enhances code flexibility, reusability and maintainability

ADVANTAGES
-allows high level of reusability due to shared methods and
attributes(encapsulation enables code reuse due to hidden internal
workings, inheritance allows reusing parts of existing classes,
polymorphism enables you to write generic code that can be reused on
different objects)

-classes can be used across multiple projects

-encapsulation makes code more reliable by protecting attributes from


being directly accessed and wrongly changed

-thorough design can produce higher quality software with fewer


vulnerabilities

-modular structure is easy to maintain and update(each class is kept


encapsulated from other classes so wont affect others since not allowed
direct access)

-high level of abstraction once classes have been created, they can be
reused
DISADVANTAGES
-alternate style of thinking

-not always suitable when few components are used

-may result in longer more inefficient program

OOP IN PSUEDOCODE
Eg a class called toytank

It has the attributes:colour and name

It has the methods:getcolour(),getname(),setcolour(),setname()

Class toytank

Private colour

Private name

Public procedure new(givencolour,givenname)

Colour=givencolour

Name=givenname

End procedure

Public procedure getcolour()

RETURN colour

End procedure

Public procedure getname()

RETURN name

End procedure

Public procedure setcolour(newcolour)

Colour=newcolour

End procedure

Public procedure setname(newname)

Name=newname

End procedure
End class

THIS ABOVE IS WHAT DEFINES THE TOYTANK CLASS ITSELF

To define an object you have to instantiate objects

Eg

Tankone= new toytank(“blue”,”trevor”)

Tanktwo= new toytank(“red”,”roger”)

Tankthree= new toytank(“yellow”,”joe”)

Tankone.getName

Tanktwo.setname(“Tom”)

Tankthree.getcolour
So when we do “objectname.super.getter()” it will use the SUPERCLASSES
METHOD

If we just do objectname.getter() and the object is instantiated into the


subclass it will output the OVERIDDEN VERSION
-you write class SUBCLASS inherits SUPERCLASS then add your private
attributes a constructor method and the extra methods it will have

Say in the main code we instantiated an object into the employee


subclass like this

Personfour=employee(“tracy”,”bath”,”1232311”)

This object will have 3 attributes and 8 methods

-you can go on to keep inheriting from superclasses


Remember if we call say the method get name that has the SAME NAME
but different code it will use the subclasses method unless if we use
super.getname() otherwise the superclass will get overridden

if you want to override the superclass method you have to REWRITE the
method in the subclass

ENCAPSULATION
In the example from above say you instantiated a new person like this

Persontwo=newperson(“roger”,”bath”)

This works BUT IF YOU TRY DOING THIS

Persontwo.name=”hoola”

This wont work you can not directly access attributes and assign its value
in this way

WE CAN use setter methods this often have a form of validation eg

Persontwo.setname(“Ronald”)

The method may check the length of the name and its validity

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy