Lec 01 [oop basics]

Download Lec 01 [oop basics]

Post on 02-Jul-2015




1 download


  • 1. BITS PilaniPilani CampusAvinash GautamBITS PilaniDepartment of Computer Science and Information Systems

2. BITS PilaniPilani CampusObject-Oriented Programming (BITS C342)Avinash Gautamavinash@bits-pilani.ac.inChamber# 3254-GConsultation HourBy appointment through mail 3. BITS Pilani, Pilani CampusLecture-1 [OOP Basics]3OOP Concepts What is an object? What is a Class? What is Inheritance? What is an Interface?BITS C342, Object Oriented Programming 4. BITS Pilani, Pilani CampusOOP ConceptsBITS C342, Object Oriented Programming4 5. BITS Pilani, Pilani CampusWhat is an object? Look around you and identify some objectsReal world objects share two characteristics They all have state They all have behavior Identifying the state and behavior for real-world objects is a greatway to begin thinking in terms of object-oriented programming Two Questions What possible states can this object be in? What possible behavior can this object perform? Write down your observationReal world objects vary in complexity (Radio, Table Lamp) Objects may contain other objects These real world observations all translate into object orientedprogrammingBITS C342, Object Oriented Programming5 6. 6BITS Pilani, Pilani Campus Software objects are conceptually similar to realworld objects: they too consist of state and relatedbehavior An object store its state in fields and exposes itsbehavior through methods Methods operate on objects internal state and serveas the primary mechanism for object-to-objectcommunication Hiding internal state and requiring all interactionthrough an objects method is known as dataencapsulation - a fundamental principle of OOPBITS C342, Object Oriented Programming 7. 7BITS Pilani, Pilani CampusWhat is an object? (Contd.) Bicycle - By attributing state and providingmethods for changing that state, the objectremains in control of how the outsideworld is allowed to use it. For example, ifthe bicycle only has 6 gears, a method tochange gears could reject any value that isless than 1 or greater than 6. Benefits of building code into individualsoftware objects Modularity Information-hiding Code re-use Plug-ability and debugging easeBITS C342, Object Oriented Programming 8. BITS Pilani, Pilani CampusWhat is a Class? A class is a blueprint or prototype from which objects arecreated A class defines the general nature of a collection ofobjects of the same type The process creating an object from a class is calledinstantiation Every object is an instance of a particular class. There can be many instances of objects from the sameclass possible with different values for dataBITS C342, Object Oriented Programming8 9. redRoseBITS Pilani, Pilani CampusExample9class RoseblueRoseclassobjectsObjectReferencesBITS C342, Object Oriented Programming 10. BITS Pilani, Pilani CampusWhat is Inheritance? Different kinds of objects have a certain amount incommon with each other For example mountain bikes, road bikes, and tandemBikes all share the characteristics of bicycles (currentspeed, current pedal cadence, current gear) Yet each also defines additional features that make themdifferent: tandem bicycles have two seats and two setsof handlebars; road bikes have drop handlebars etc Object-oriented programming allows classes to inheritcommonly used state and behaviour from other classesBITS C342, Object Oriented Programming10 11. BITS Pilani, Pilani CampusWhat is Inheritance?BITS C342, Object Oriented Programming11 12. BITS Pilani, Pilani CampusWhat is a Interface? Objects define their interaction with the outsideworld through the methods that they expose Methods form the object's interface with theoutside world The buttons on the front of your television set,for example, are the interface between you andthe electrical wiring on the other side of itsplastic casing. You press the "power" button toturn the television on and offBITS C342, Object Oriented Programming12 13. BITS Pilani, Pilani CampusQuestions Real-world objects contain _____ and ________ A software object's state is stored in its _____ A software object's behaviour is exposed through_______ Hiding internal data from the outside world, andaccessing it only through publicly exposed methodsis known as data ___________ A blueprint for a software object is called a ____ Common behaviour can be defined in a _________and inherited into a _______BITS C342, Object Oriented Programming13state behaviourfieldsmethodsencapsulationclasssuper-classsub-class 14. 14BITS Pilani, Pilani CampusTHANK YOU1. The material presented in these slides is used only for academic purpose2. Copyright Oracle Sun MicrosystemsBITS C342, Object Oriented Programming