Lecture 04.pdf
Document Details
Uploaded by EarnestDivergence
Tags
Full Transcript
Object Oriented Concepts Lecture-04 Classes & Objects – Part 2 1 IT1050| Object Oriented Concepts| Lecture-04| Learning Outcomes At the end of the Lecture students should be able to Understand, identify and des...
Object Oriented Concepts Lecture-04 Classes & Objects – Part 2 1 IT1050| Object Oriented Concepts| Lecture-04| Learning Outcomes At the end of the Lecture students should be able to Understand, identify and describe Classes, Objects, Properties and Methods Describe Encapsulation, Information Hiding, and Interfaces 2 IT1050| Object Oriented Concepts| Lecture-04| How do we develop an OO Program ? We look at the problem that needs to be solved This is the building we want to build e.g. In a real world scenario this could be a Student Information System (SIS) 3 IT1050| Object Oriented Concepts| Lecture-04| Identifying Objects needed These are the Blocks (Objects) that we need. e.g. In SIS objects could be details of OOC, IWT, students called Manoj, Gayani 4 IT1050| Object Oriented Concepts| Lecture-04| How do you create these Blocks (Objects)? What if we needed to manufacture these blocks. How could we do this? What do we need to make first? 5 IT1050| Object Oriented Concepts| Lecture-04| A Mould (Class) Once we make a Mould (Class) we can make as many blocks (Object) that we need. 6 IT1050| Object Oriented Concepts| Lecture-04| How to group these Blocks (Objects)? How can we group these Blocks? 7 IT1050| Object Oriented Concepts| Lecture-04| We could do it by Shape A Square Mould (class) A Rectangle Mould (class) e.g. In SIS it could be Student Class, Subject Class 8 IT1050| Object Oriented Concepts| Lecture-04| Creating Blocks (Objects) from Moulds (Classes) A Square Mould (class) A Rectangle Mould (class) Blocks (Objects) made 9 IT1050| Object Oriented Concepts| Lecture-04| Final Product – Assembling Blocks (Objects) We can now assemble the Objects and create our final solution. 10 IT1050| Object Oriented Concepts| Lecture-04| Problem to Solve Identify Objects that are needed Assemble Objects to create the solution Create Objects from Identify Classes through Abstraction Classes 11 IT1050| Object Oriented Concepts| Lecture-04| I am dreaming of How do you build a house? 12 IT1050| Object Oriented Concepts| Lecture-04| Meeting an Architect to make a House Plan (Blue Print) 13 IT1050| Object Oriented Concepts| Lecture-04| Blue Print – House Plan (Class) Your Dream House Class House 14 IT1050| Object Oriented Concepts| Lecture-04| Your Dream House With the House Plan – Blue Print (Class) you can now get a contractor to build your dream house (Object) Class House Object 15 IT1050| Object Oriented Concepts| Lecture-04| Classes and Objects An Object is a specific instance (variable) of the data type (class) A class is a blue print of an object. Class House Object 16 IT1050| Object Oriented Concepts| Lecture-04| Classes and Objects You can make as many houses as you want from a single house plan – Blue Print (Class) House2 House1 Objects Class House House3 17 IT1050| Object Oriented Concepts| Lecture-04| Classes – Captures Behavior as well A concept (class) has both properties and behavior. We know that dogs and cats behave differently Cat Class Dog Class (c) 2017 Monique Snoeck, KU LEUVEN 18 IT1050| Object Oriented Concepts| Lecture-04| Example – Behaviour is captured as functions Dog Class Cat Class name name owner owner breed breed work (e.g. police dog) Bark() Meow() Fetch() Purr() Grouping properties and functions together is called Encapsulation 19 IT1050| Object Oriented Concepts| Lecture-04| Rectangle Class Rectangle Class Name length width Properties (Attributes) calcArea () calcPerimeter() getLength() getWidth() Functions setLength() (Methods) setWidth() (Behaviour) 20 IT1050| Object Oriented Concepts| Lecture-04| Employee Class Employee EmpNo Class name Name Address Attributes Properties BasicSalary Data Members OtHrs OtRate Methods CalculateOTAmount() Behaviors CalculateNetSalary() member functions PrintPaySlip() 21 IT1050| Object Oriented Concepts| Lecture-04| Restricted Access All properties and some functions of a Class have restricted access (private) and can be accessed only through public functions. Why is this necessary? Let’s look at an example. 22 IT1050| Object Oriented Concepts| Lecture-04| A Jewelry Shop 23 IT1050| Object Oriented Concepts| Lecture-04| Jewelry can be accessed only through a Sales Person 24 IT1050| Object Oriented Concepts| Lecture-04| JewelleryShop Class JewelleryShop Class gems Private jewellery (Restricted) viewJewellery() buyJewellery() Public (Salesman do this) 25 IT1050| Object Oriented Concepts| Lecture-04| Information Hiding Hide certain information or implementation decision that are internal to the encapsulation structure ( class ) The only way to access an object is through its public interface (public functions) Public – anyone can access / see it Private – no one except the class can see/ use it 26 IT1050| Object Oriented Concepts| Lecture-04| Encapsulation It is the process of grouping related attributes and methods together, giving a name to the unit and providing an interface (public functions) for outsiders to communicate with the unit. 27 IT1050| Object Oriented Concepts| Lecture-04| Encapsulation The implementation of the TV is hidden from us. Your TV could be OLED, LCD, Plasma or an old CRT one. You can control any such TV using the same commands in your remote (interface). 28 IT1050| Object Oriented Concepts| Lecture-04| Encapsulation ( Reference : Grady Booch, eta (2008), Object Oriented Analysis and Design with Applications 3rd Edition, pg 52) 29 IT1050| Object Oriented Concepts| Lecture-04| Encapsulation Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation. ( Reference : Grady Booch, eta (2008), Object Oriented Analysis and Design with Applications 3rd Edition, pg 52) 30 IT1050| Object Oriented Concepts| Lecture-04| Interface – Public Functions You interact with some object through its interfaces. Your car can be Gasoline, Hybrid or Electric but you drive it the same way. 31 IT1050| Object Oriented Concepts| Lecture-04| Reference Chapter 03 Grady Booch (2008), Object-Oriented Analysis and Design with Application, 3rd Edition Chapter 09 Deitel & Deitel’s (2016), C++ How to Program, 9th Edition 32 IT1050| Object Oriented Concepts| Lecture-04|