The document outlines the principles of Object Oriented Programming (OOP) in Java, contrasting it with Procedure Oriented Programming (SOP). Key concepts include classes, objects, encapsulation, abstraction, inheritance, and polymorphism, emphasizing the focus on data in OOP compared to the process in SOP. The document also discusses the advantages of OOP, such as increased security, reusability, and flexibility.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
2 Principles of OOP Java
The document outlines the principles of Object Oriented Programming (OOP) in Java, contrasting it with Procedure Oriented Programming (SOP). Key concepts include classes, objects, encapsulation, abstraction, inheritance, and polymorphism, emphasizing the focus on data in OOP compared to the process in SOP. The document also discusses the advantages of OOP, such as increased security, reusability, and flexibility.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27
Principles of Object Oriented
Programming in Java Mr. M.R. Solanki Sr. Lecturer, Information Technology, [email protected] SBMP Learning Outcomes
Students will be able to:
• Describe types of programming Languages • Explain the difference between SOP and OOP • Define class, objects, abstraction, encapsulation, inheritance and polymorphism • Write a sample C++ Program Programming Languages Types of Programming Languages: 1. Low (Binary or / Machine ) Level Language: 0’s and 1’s Input and output are in binary form Efficient performance as Processor knows binary Difficult to learn (Only scientists and researchers could work with it) Programming Languages Types of Programming Languages: 2. Assembly Level Language: Hexadecimal Number System (0-F) Easy to learn so user friendly than binary Not efficient as binary because of Assembler required to translate code into binary and extra cost for the assembler software Programming Languages Types of Programming Languages: 3. Higher Level Language: Decimal Number System (0-9) Most user friendly because of fully English like structure Performance is worse because Compiler or Interpreter is required for translation from higher level to lower level Easy to learn compared to other languages Programming Languages Types of Programming Languages: Timeline of HLLs Some Popular HLLs Structured v/s Object Oriented Programming Language Procedure Oriented Program Procedure-Oriented programming basically consists of writing a list of instructions (or actions) for the computer to follow, and organizing these instructions into groups known as functions. While we concentrate on the development of functions, very little attention is given to the data t hat are being used by various functions. What happens to the data? How are they affected by the functions that work on them? In a multi function program, many important data items are placed as global so that they may be accessed by all the functions Global data are more vulnerable to an accidental change by a function Procedure Oriented Program
Typical Structure of Procedure Oriented Program
Procedure Oriented Program
Data and Functions in SOP
Object Oriented Program The major motivating factor in the invention of object oriented approach is to remove some of the flaws encountered in the procedural approach. OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. It ties data more closely to the function that operate on it, and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. The data of an object can be accessed only by the functions associated with that object. Object Oriented Program
Organization of Data in OOP
SOP v/s OOP Sr. Structured Programming Object Oriented Programming No Language (SOP) Language(OOP) 1 It is designed to focus It is designed to focus on data. on process/ logical structure and then data required for that process. 2 Follows top-down approach. Follows bottom-up approach. 3 Also known as Modular Supports inheritance, Programming and a subset encapsulation, of procedural programming abstraction, polymorphism, etc. language. 4 Programs are divided into small Programs are divided into small self-contained functions. entities called objects. 5 Function transform data from one Functions that operate on data of form to another an object are tied together in a data structure SOP v/s OOP 6 Less secure as there is no way More secure as having data of data hiding. (most of the hiding feature through access functions share global data) specifiers/modifiers i.e. private and protected 7 Can solve moderately complex Can solve programs. any complex programs. 8 Provides less reusability, more Provides more reusability (due to function dependency. inheritance), less function dependency. 9 Less abstraction and less More abstraction and flexibility. more flexibility( new data can functions can be added easily whenever required) 10 Examples: FORTRAN, C, BASIC, Examples: C++, Java, PHP, .Net, PASCAL, etc. Python, etc. OOP Principles class We can create a user defined data type in Java using class. A class can be considered as a “Template” or “Blue Print” or “Dye” which declares data and code(function) inside it. A class is a logical reality from which we can create n no. of instances i.e. from a dye, we can create so many idols, from a blue print (plan/design/pattern), we can construct so many buildings physically, etc. Thus a class is a collection of objects of similar types. object Basic run time entities in OOP i.e. a person, a bank, an employee, a student, etc. Are variables of type class Is a physical reality Each object contains data and code(function) to manipulate data Objects interact with each other by sending messages. i.e. a customer object can request an account object for the bank balance. Data Encapsulation The wrapping up of data and functions into a single unit (class) is known as Encapsulation Only functions declared inside the class can access the data. (outside interference is restricted – security) Also called as “Data Hiding” or “Information Hiding” The functions provide an interface between an object’s data and the program. Encapsulation is achieved trough access modifiers private and protected in Java. Data Encapsulation Example: class Employee { // data members private int code; private String name; private float salary; private int experience; // member methods void setEmp() { // data input } void getEmp() { // data output } } // end of class Data Encapsulation class EmployeeMain { public static void main(String args[]) { Employee e=new Employee(); e.code=1; // error becuase of private access specifier } } Data Abstarction Act of representing essential features without including the internal details or explanations. Examples: i. To drive a car without knowledge of Automobile Engineering ii. Using library functions in C/C++/Java - pow(), sqrt(), etc. iii. To use Internet, Smart Phones without knowing internal technicalities. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost. Functions to operate on these attributes i.e. employee class To use the functionality of employee class, user is not worried about internal details rather he has to create an object and call the member functions. Data Abstraction Example: class Employee { // data members private int code; private String name; private float salary; private int experience; // member methods void setEmp() { // data input } void getEmp() { // data output } } // end of class Thanks! Any questions? You can find me at: [email protected] Credits Object oriented programming with C++, E Balagurusamy, Tata McGraw-Hill Publishing Company Limited, New Delhi