CHAPTER - 04 OBJECT ORIENTED PROGRAMMING
Unit I Programming and Computational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
INTRODUCTION
INTRODUCTION An object-oriented programming (OOP) is a programming language model which is organized around "objects" rather than "actions" and data rather than logic. Before the introduction of the Object Oriented Programming paradigm, a program was viewed as a logical procedure that takes input data, processes it, and produces output. But in case of OOP a problem is viewed in terms of objects rather than procedure for doing it.
BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
The basic concepts related to OOP are as follows: 1. Objects 2. Classes 3. Encapsulation 4. Abstraction 5. Data Hiding 6. Polymorphism 7. Inheritance BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
1. Objects An object is the basic key concept of Object Oriented Programming. it can be anything around us - a person, place, any activity or any other identifiable entity. Every object is characterised by i) identity ii) Properties iii) Behaviour i. Identity : This is the name that identifies an object. For example a Student is the name given to anybody who is pursuing a course BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
ii. Properties: These are the features or attributes of the object For example a student will have his name, age, class, date of birth etc. as his attributes or properties. iii. Behaviour: The behaviour of an object signifies what all functions an object can perform. For example a student can pass or fail the examination. A mobile phone can click and store photographs (behave like a camera). BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
What is Class? A class is group of objects with same attributes and common behaviours. It is basically a blueprint to create objects. An object is a basic key concept of OOP but classes provide an ability to generalize similar type of objects. Both data and functions operating on the data are bundled as a unit in a class for the same category of objects. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
for example of the class Mobile_phone which is represented in the block diagram below BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
A class is defined before the creation of objects of its type. The objects are then created as instances of this class as shown in the figure. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
A class is defined before the creation of objects of its type. The objects are then created as instances of this class as shown in the figure. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING OBJECTS
A real instance of a class is called an object and creating the new object is called instantiation. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING WHAT IS INSTANTIATION?
OOP FEATURES Encapsulation Data Hiding Data Abstraction Inheritance Polymorphism
OOP FEATURES Encapsulation Encapsulation is the most basic concept of OOP. It is the combining of data and the functions associated with that data in a single unit. In most of the languages including python, this unit is called a class. In Fig showing class Mobile_phone, given under the subtopic Classes, we see that the name of the class, its properties or attributes and behaviours are all enclosed under one independent unit. This is encapsulation, implemented through the unit named class.
OOP FEATURES Data Hiding Data hiding can be defined as the mechanism of hiding the data of a class from the outside world or to be precise, from other classes. This is done to protect the data from any accidental or intentional access. In most of the object oriented programming languages, encapsulation is implemented through classes. In a class, data may be made private or public.
OOP FEATURES Data Hiding Private data or function of a class cannot be accessed from outside the class while public data or functions can be accessed from anywhere. So data hiding is achieved by making the members of the class private. Access to private members is restricted and is only available to the member functions of the same class.
OOP FEATURES Data Abstraction Do you know the inner details of the monitor of your PC or your mobile phone? What happens when you switch ON the monitor or when any call is received by you on your phone? Does it really matter to you what is happening inside these devices? No, it does not. Right? Important thing for you is whether these devices are working as per your requirement or not?
OOP FEATURES Data Abstraction You are never concerned about their inner circuitry. This is what we call abstraction. The process of identifying and separating the essential features without including the internal details is abstraction. Only the essential information is provided to the outside world while the background details are hidden.
OOP FEATURES Data Abstraction Classes use the concept of abstraction. A class encapsulates the relevant data and functions that operate on data by hiding the complex implementation details from the user. The user needs to focus on what a class does rather than how it does.
OOP FEATURES Data Abstraction Abstraction and Encapsulation are complementary concepts. Through encapsulation only we are able to enclose the components of the object into a single unit and separate the private and public members. It is through abstraction that only the essential behaviours of the objects are made visible to the outside world. So we can say that encapsulation is the way to implement data abstraction.
OOP FEATURES Inheritance Inheritance is one of the most useful characteristic of object-oriented programming as it enforces reusability of code. Inheritance is the process of forming a new class (derived class) from an existing class (called the base class). The data members and the methods associated with the data are accessible in the inherited class.
OOP FEATURES Inheritance
OOP FEATURES Polymorphism The word Polymorphism is formed from two words - poly and morph where poly means many and morph means forms. So polymorphism is the ability to use an operator or function in various forms. That is a single function or an operator behaves differently depending upon the data provided to them. Polymorphism can be achieved in two ways:
OOP FEATURES Polymorphism Polymorphism can be achieved in two ways: 1. Operator Overloading 2. Function Overloading
OOP FEATURES Polymorphism 1. Operator Overloading: The '+' operator behaves differently with different data types. With integers it adds the two numbers and with strings it concatenates or joins two strings. For example: Print 8+9 will give 17 and Print "Python" + "programming" will give the output as Pythonprogramming. This feature where an operator can be used in different forms is known as Operator Overloading and is one of the methods to implement polymorphism.
OOP FEATURES Polymorphism 2. Function Overloading: Polymorphism in case of functions is a bit different. A named function can also vary depending on the parameters it is given. For example, we define multiple functions with same name but different argument list as shown below:
OOP FEATURES Polymorphism def test(): #function 1 print "hello" def test(a, b): #function 2 return a+b def test(a, b, c): #function 3 return a+b+c
Static and Dynamic Binding Binding is the process of linking the function call to the function definition. The body of the function is executed when the function call is made. Binding can be of two types: 1. Static Binding. 2. Dynamic Binding.
Static Binding In this type of binding, the linking of function call to the function definition is done during compilation of the program.
Dynamic Binding In this type of binding, linking of a function call to the function definition is done at run time. That means the code of the function that is to be linked with function call is unknown until it is executed. Dynamic binding of functions makes the programs more flexible.
Advantages of OOP Object Oriented programming has following advantages: 1. Simplicity. 2. Modifiability. 3. Extensibility and Maintainability. 4. Re-usability. 5. Security.
Advantages of OOP The objects in case of OOP are close to the real world objects, so the complexity of the program is reduced making the program structure very clear and simple. For example by looking at the class Mobile_phone, you can simply identify with the properties and behaviour of an actual mobile phone. This makes the class Mobile_phone very simple and easy to understand. 1. Simplicity
Advantages of OOP It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods. 2. Modifiability
Advantages of OOP It is quite easy to add new features and extend the program in case of object oriented programming. It can be simply done by introducing a few new objects and modifying some existing ones. The original base class need not be modified at all. Even objects can be maintained separately. There by making locating and fixing problems easier. 3. Extensibility and Maintainability
Advantages of OOP For example if a new version of i- phone is introduced, a new derived class of the class i_phone for the new version may be created and no other class in the class hierarchy need to be modified. Similarly if any behaviour of a Windows phone changes, maintenance has to be done only for the class Windows phone 3. Extensibility and Maintainability
Advantages of OOP Objects can be reused in different programs. The class definitions can be reused in various applications. Inheritance makes it possible to define subclasses of data objects that share some or all of the main class characteristics. It forces a more thorough data analysis, reduces development time, and ensures more accurate coding. 4. Re-usability
Advantages of OOP Since a class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data. This characteristic of data hiding provides greater system security and avoids unintended data corruption. 5. Security
CLASS TEST 1. Explain in detail the object oriented programming 2. What is polymorphism? Explain 3. Explain Data Abstraction 4. What is Data Hiding? 5. Explain in detail static and dynamic binding Class : XII Time: 40 Min Topic: OOPS Max Marks: 20 Each Question carries 5 marks
Thank You

Chapter 04 object oriented programming

  • 1.
    CHAPTER - 04 OBJECTORIENTED PROGRAMMING
  • 2.
    Unit I Programming andComputational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
  • 3.
  • 4.
    INTRODUCTION An object-oriented programming(OOP) is a programming language model which is organized around "objects" rather than "actions" and data rather than logic. Before the introduction of the Object Oriented Programming paradigm, a program was viewed as a logical procedure that takes input data, processes it, and produces output. But in case of OOP a problem is viewed in terms of objects rather than procedure for doing it.
  • 5.
    BASIC CONCEPTS OFOBJECT ORIENTED PROGRAMMING
  • 6.
    The basic conceptsrelated to OOP are as follows: 1. Objects 2. Classes 3. Encapsulation 4. Abstraction 5. Data Hiding 6. Polymorphism 7. Inheritance BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
  • 7.
    1. Objects An objectis the basic key concept of Object Oriented Programming. it can be anything around us - a person, place, any activity or any other identifiable entity. Every object is characterised by i) identity ii) Properties iii) Behaviour i. Identity : This is the name that identifies an object. For example a Student is the name given to anybody who is pursuing a course BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
  • 8.
    ii. Properties: Theseare the features or attributes of the object For example a student will have his name, age, class, date of birth etc. as his attributes or properties. iii. Behaviour: The behaviour of an object signifies what all functions an object can perform. For example a student can pass or fail the examination. A mobile phone can click and store photographs (behave like a camera). BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
  • 9.
    What is Class? Aclass is group of objects with same attributes and common behaviours. It is basically a blueprint to create objects. An object is a basic key concept of OOP but classes provide an ability to generalize similar type of objects. Both data and functions operating on the data are bundled as a unit in a class for the same category of objects. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
  • 10.
    for example ofthe class Mobile_phone which is represented in the block diagram below BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
  • 11.
    A class isdefined before the creation of objects of its type. The objects are then created as instances of this class as shown in the figure. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING
  • 12.
    A class isdefined before the creation of objects of its type. The objects are then created as instances of this class as shown in the figure. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING OBJECTS
  • 13.
    A real instanceof a class is called an object and creating the new object is called instantiation. BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING WHAT IS INSTANTIATION?
  • 14.
    OOP FEATURES Encapsulation Data Hiding DataAbstraction Inheritance Polymorphism
  • 15.
    OOP FEATURES Encapsulation Encapsulation isthe most basic concept of OOP. It is the combining of data and the functions associated with that data in a single unit. In most of the languages including python, this unit is called a class. In Fig showing class Mobile_phone, given under the subtopic Classes, we see that the name of the class, its properties or attributes and behaviours are all enclosed under one independent unit. This is encapsulation, implemented through the unit named class.
  • 16.
    OOP FEATURES Data Hiding Datahiding can be defined as the mechanism of hiding the data of a class from the outside world or to be precise, from other classes. This is done to protect the data from any accidental or intentional access. In most of the object oriented programming languages, encapsulation is implemented through classes. In a class, data may be made private or public.
  • 17.
    OOP FEATURES Data Hiding Privatedata or function of a class cannot be accessed from outside the class while public data or functions can be accessed from anywhere. So data hiding is achieved by making the members of the class private. Access to private members is restricted and is only available to the member functions of the same class.
  • 18.
    OOP FEATURES Data Abstraction Doyou know the inner details of the monitor of your PC or your mobile phone? What happens when you switch ON the monitor or when any call is received by you on your phone? Does it really matter to you what is happening inside these devices? No, it does not. Right? Important thing for you is whether these devices are working as per your requirement or not?
  • 19.
    OOP FEATURES Data Abstraction Youare never concerned about their inner circuitry. This is what we call abstraction. The process of identifying and separating the essential features without including the internal details is abstraction. Only the essential information is provided to the outside world while the background details are hidden.
  • 20.
    OOP FEATURES Data Abstraction Classesuse the concept of abstraction. A class encapsulates the relevant data and functions that operate on data by hiding the complex implementation details from the user. The user needs to focus on what a class does rather than how it does.
  • 21.
    OOP FEATURES Data Abstraction Abstractionand Encapsulation are complementary concepts. Through encapsulation only we are able to enclose the components of the object into a single unit and separate the private and public members. It is through abstraction that only the essential behaviours of the objects are made visible to the outside world. So we can say that encapsulation is the way to implement data abstraction.
  • 22.
    OOP FEATURES Inheritance Inheritance isone of the most useful characteristic of object-oriented programming as it enforces reusability of code. Inheritance is the process of forming a new class (derived class) from an existing class (called the base class). The data members and the methods associated with the data are accessible in the inherited class.
  • 23.
  • 24.
    OOP FEATURES Polymorphism The wordPolymorphism is formed from two words - poly and morph where poly means many and morph means forms. So polymorphism is the ability to use an operator or function in various forms. That is a single function or an operator behaves differently depending upon the data provided to them. Polymorphism can be achieved in two ways:
  • 25.
    OOP FEATURES Polymorphism Polymorphism canbe achieved in two ways: 1. Operator Overloading 2. Function Overloading
  • 26.
    OOP FEATURES Polymorphism 1. OperatorOverloading: The '+' operator behaves differently with different data types. With integers it adds the two numbers and with strings it concatenates or joins two strings. For example: Print 8+9 will give 17 and Print "Python" + "programming" will give the output as Pythonprogramming. This feature where an operator can be used in different forms is known as Operator Overloading and is one of the methods to implement polymorphism.
  • 27.
    OOP FEATURES Polymorphism 2. FunctionOverloading: Polymorphism in case of functions is a bit different. A named function can also vary depending on the parameters it is given. For example, we define multiple functions with same name but different argument list as shown below:
  • 28.
    OOP FEATURES Polymorphism def test():#function 1 print "hello" def test(a, b): #function 2 return a+b def test(a, b, c): #function 3 return a+b+c
  • 29.
    Static and DynamicBinding Binding is the process of linking the function call to the function definition. The body of the function is executed when the function call is made. Binding can be of two types: 1. Static Binding. 2. Dynamic Binding.
  • 30.
    Static Binding In thistype of binding, the linking of function call to the function definition is done during compilation of the program.
  • 31.
    Dynamic Binding In thistype of binding, linking of a function call to the function definition is done at run time. That means the code of the function that is to be linked with function call is unknown until it is executed. Dynamic binding of functions makes the programs more flexible.
  • 32.
    Advantages of OOP ObjectOriented programming has following advantages: 1. Simplicity. 2. Modifiability. 3. Extensibility and Maintainability. 4. Re-usability. 5. Security.
  • 33.
    Advantages of OOP Theobjects in case of OOP are close to the real world objects, so the complexity of the program is reduced making the program structure very clear and simple. For example by looking at the class Mobile_phone, you can simply identify with the properties and behaviour of an actual mobile phone. This makes the class Mobile_phone very simple and easy to understand. 1. Simplicity
  • 34.
    Advantages of OOP Itis easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods. 2. Modifiability
  • 35.
    Advantages of OOP Itis quite easy to add new features and extend the program in case of object oriented programming. It can be simply done by introducing a few new objects and modifying some existing ones. The original base class need not be modified at all. Even objects can be maintained separately. There by making locating and fixing problems easier. 3. Extensibility and Maintainability
  • 36.
    Advantages of OOP Forexample if a new version of i- phone is introduced, a new derived class of the class i_phone for the new version may be created and no other class in the class hierarchy need to be modified. Similarly if any behaviour of a Windows phone changes, maintenance has to be done only for the class Windows phone 3. Extensibility and Maintainability
  • 37.
    Advantages of OOP Objectscan be reused in different programs. The class definitions can be reused in various applications. Inheritance makes it possible to define subclasses of data objects that share some or all of the main class characteristics. It forces a more thorough data analysis, reduces development time, and ensures more accurate coding. 4. Re-usability
  • 38.
    Advantages of OOP Sincea class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data. This characteristic of data hiding provides greater system security and avoids unintended data corruption. 5. Security
  • 39.
    CLASS TEST 1. Explainin detail the object oriented programming 2. What is polymorphism? Explain 3. Explain Data Abstraction 4. What is Data Hiding? 5. Explain in detail static and dynamic binding Class : XII Time: 40 Min Topic: OOPS Max Marks: 20 Each Question carries 5 marks
  • 40.