Object Oriented Programming Mr Cleary What is OOP
Object Oriented Programming Mr Cleary
What is OOP? �Object Oriented Programming allows data and procedures to be packaged together using elements called objects, which are single units.
Intro to OOP Imagine you’re a child and you have a pile of toys like this. I then ask you to find a green car, however…. On one condition you should not disturb the toys and leave the pile as it is. It’s difficult!
Intro to OOP Let’s sort the toys and put them in boxes like this:
Intro to OOP And arrange them boxes on a shelf. I now ask you to find the green car.
Intro to OOP In earlier days a computer program was a long list of commands, just like our lump of toys.
Intro to OOP The commands were then grouped into a list of commands called functions. (Just like this)
Procedural Programs All the data was in one location and it was modified from everywhere. Procedural programming forced developers to write highly interdependent code. Problems: Data modified in one function would be modified in another function. It was difficult to change things or fix debugs. Changing the program was a nightmare.
Procedural vs OOP �In OOP you can have function calls and logic can go all over the place instead of just from top to bottom
OOP �Before object-oriented programming, computer languages had two basic parts. . . variables (places to store data) and functions that operate on those languages. An object combines the variables and the functions that work on them into one unit.
Classes �A class is a data structure that can hold both data and functions; a template for creating objects. The class produces objects.
Classes and objects �Object (something produced according to that template) �A specific fruit could be an object of the class fruit e. g. apple
An example �Think of a real-world object, such as a cat. A cat could be said to have properties (or states), such as name, age, and colour. �A cat also has behaviours such as sleeping, eating, and purring. In the world of OOP, objects also have properties and behaviours.
Cats analogy �Consider that there are cats of different colours, ages, and names, with different ways of eating and purring. But despite their individual differences, all cats are members of the same category, or in OOP terms, the same class: the class of cats. In OOP terminology, each individual cat is said to be an instance of the Cat class.
Inheritance �Is a relationship of inheriting the attributes and behaviours from the parent class. �Dog is an animal. �Parent and Child Class. �Dog is inheriting Animal attributes.
Inheritance �Parent classes often provide some rough ideas on what they should be while child classes usually develop that idea further. For example a specific behaviour of dog would be to bark and you would not put that in the animal class because we don't know what animal it is. �Derived Class
Inheritance Animal Cat �Share Dog Tiger common characteristics like eyes, nose, paws, ears.
Inheritance Animal Cat Dog � Classes Tiger inherit another class by extending. In JAVA we would write: Public class Dog extends Animal � All methods are inherited.
When to use Inheritance? �Generally, when 3 or more things are related to each other in someway that is useful in the program, then inheritance would be appropriate. �When using inheritance, make sure the 2 classes (parent and child) has a 'Is a' relationship.
The ‘Is a’ relationship �The 'Is a' relationship: As yourself is child class a parent class? Is duck an animal? If so, the inheritance is appropriate. Is animal a duck? No, inheritance doesn't happen viceversa.
Detailed Diagram Animal �The Mamm al Birds Monkey Duck child class can become the parent of another class.
Detailed Diagram Animal �No Mamm al Birds Monkey Duck such thing as multiple inheritance.
Advantages: �Minimise �Main duplicate code. disadvantage of using inheritance is that the two classes (base and inherited class) get tightly coupled. This means one cannot be used independent of each other.
- Slides: 23