Lesson Objectives Aims Key Words: To be able to understand the Object Oriented paradigm To be able to understand key OO terms Key Words: Class, Method, Constructor, Inheritance, Polymorphism
Object Orientation A method of programming where real world or abstract concepts are represented as “Objects” An object can be anything:
Exam board definition Class: A template Which defines methods and attributes Of/used to make objects
Objects An object is defined in a “class” A class is: A definition of the object A constructor used to make new versions of the object A set of methods (things it can do) An object should be seen as a template – you never use the class itself, you create copies of it by making NEW objects to use in your code.
A Class A class will contain: The name of the class A constructor (code that is automatically called and run when a new instance of an object is created) Attributes (Data stored in variables) Methods (things the class can do, including getting and setting data)
Exam board definition Attribute: A variable or piece of data in a class
When designing a class, you must decide: What data does the object need to store What data does the object need when it is created What does it need to be capable of doing? Lets do an example.
Task We want to make a simple database to store details of cars we sell Each car has: A make A model Purchase Price Mileage It would be nice if we could do/find out the following: Add or retrieve the list price of the car Add or retrieve the age of the car Calculate the market value of the car based on age, list price and depreciation Display all car details
We are going to create a Car class. Task We are going to create a Car class. Write out a class definition in a table: Object Name Properties (values it stores) Methods (things you want it to be able to do)
Example Object Name Car Properties (values it stores) Make as String Model as String Mileage as Integer Age as integer ListPrice as double PurchasePrice as double Methods (things you want it to be able to do) GetAllDetails() SetListPrice(Price as double) GetlListPrice() CalculateDepreciation() getAge() Setage(Year as integer)
How a class looks Lets have a look at a outline class declaration together Then you can try and fill in your own We will then work together to write the code …and some test code to try it out
Inheritance If an object exists, but doesn’t do exactly what you need… …or isn’t specific enough for your needs then… …take the code and bolt on your own bits
Inheritance is a class which takes ALL of the functionality of an existing class All of the methods, data storage etc And allows you to build on it, change methods etc. This is done in a new class of its own so as not to damage or alter the “base” class
Inheritance terms The base class is called the “parent” class Any inheriting classes are called “Child” classes Time for an example
Example Parent class Person Person has attributes Fname, Sname and age Methods – Getname(), setname(), GetFullname()
Example Child Class Employee Has all attributes and methods of Person Adds attributes – Position, salary Adds methods – getposition(), setSalary() etc
Exam board definition Inheritance: A class which takes on the methods And attributes Of a parent class. An inheriting class may override some of the methods/attributes (polymorphism) And may have additional methods and attributes of its own
Polymorphism Functions or methods that have the same name But can be called in different ways Concatenate(23, 154) Concatenate(“Chunder “, “Jacket”)
Why OO? We create abstract code blocks that can be re-used Existing classes can be modified through inheritance to create classes for our own needs, but with minimal work Powerful method of storing and manipulating program code Automatically breaks down development in to manageable chunks
Continued Encapsulation allows all data to be protected by code you write For example, you cannot change a value without calling a method. This is “strong abstraction” – you are kept away from the raw data and must pass it through a method to access it
Review/Success Criteria You should know: What an object is What the terms Class, Method, Property, inheritance and polymorphism mean How to create a basic class How to define a basic class