Object Oriented Programming
Object Oriented 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
Set reserved and set onlon are a special method called setters it can set
the value of a particular attribute of an object
ATTRIBUTES ARE PRIVATE they can not directly be accessed and edited by
the users (encapsulation) they can only be altered via public methods
-idea of hiding data within a class that anything outside that class cannot
directly interact with
ABSTRACTION
-only showing essential details(encapsulation hides internal details and
exposes only necessary functionality)
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
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
Basically method with the same name and parameters but providing
different functionality depending on if its in the sub or super class
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)
-high level of abstraction once classes have been created, they can be
reused
DISADVANTAGES
-alternate style of thinking
OOP IN PSUEDOCODE
Eg a class called toytank
Class toytank
Private colour
Private name
Colour=givencolour
Name=givenname
End procedure
RETURN colour
End procedure
RETURN name
End procedure
Colour=newcolour
End procedure
Name=newname
End procedure
End class
Eg
Tankone.getName
Tanktwo.setname(“Tom”)
Tankthree.getcolour
So when we do “objectname.super.getter()” it will use the SUPERCLASSES
METHOD
Personfour=employee(“tracy”,”bath”,”1232311”)
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”)
Persontwo.name=”hoola”
This wont work you can not directly access attributes and assign its value
in this way
Persontwo.setname(“Ronald”)
The method may check the length of the name and its validity