Object-Oriented Programming (OOP) is the term used to
describe a programming approach based on objects and classes. The object-oriented paradigm allows us to organize software as a collection of objects that consist of both data and behavior. This is in contrast to conventional functional programming practice that only loosely connects data and behavior. An object-oriented application uses a collection of objects, which communicate by passing messages to request services. Objects are capable of passing messages, receiving messages, and processing data. INTRODUCTION TO OOP
OOP allows decomposition of a problem into a number of
entities called objects and then builds data and behaviors around these objects. The software is divided into a number of small units called objects. The data and behaviors are built around these objects. The data of the objects can be accessed only by the behaviors associated with that object. The behaviors of one object can access the behaviors of another object. BRIEF HISTORY OF OOP Simula 67 – the first OO programming language; extension of ALGOL60 Smalltalk – conceived by Alan Kay (Smalltalk-72, Smalltalk-80); dynamically typed; Strongtalk (1993) – Smalltalk + type system mid 80’s – many languages added support for OO: Objective C, C++, Object Pascal, Modula 3, Oberon, Objective CAML, CLOS. Eiffel – Bertrand Meyer (1988) – Pascal-like syntax, design-by-contract Other “exotic” OO languages: Sather, Trellis/Owl, Emerald, Beta (evolution of Simula), Self Java – James Gosling (1995); Java 1.5 (2004) – support for generic programming (Theoretical) extensions to Java: e.g. GJ (1998) PROCEDURAL VS. OBJECT-ORIENTED PROGRAMMING
Object-oriented programming is the successor of procedural (structural)
programming. Procedural programming describes programs as groups of reusable code units (procedures) which define input and output parameters. Procedural programs consist of procedures, which invoke each other. In procedural languages (i.e. C), procedures are a sequence of imperative statements, such as assignments, tests, loops and invocations of sub procedures. These procedures are functions, which map arguments to return statements. PROCEDURAL VS. OBJECT-ORIENTED PROGRAMMING
The problem with procedural programming is that code reusability is
hard and limited. The difficulties with this type of programming, is that software maintenance can be difficult and time consuming. When changes are made to the main procedure (top), those changes can cascade to the sub procedures of main, and the sub-sub procedures and so on, where the change may impact all procedures in the pyramid. The aim of object-oriented programming is to try to increase the flexibility and maintainability of programs. Because programs created using an OO language are modular, they can be easier to develop, and simpler to understand after development. OOP FUNDAMENTAL: UNDERSTANDING CLASS & OBJECT
In object-oriented terminology, a class is a template for
defining objects. Class defines object data (attributes/properties) & object behaviors (methods/functions). Multiple objects, or instances of a class can be created from a single class. OOP FUNDAMENTAL: CLASS
Class is a Template/Blueprint/Plans for defining objects.
Classes describes the characteristics of the object – in JAVA programming they are referred as data or field – private double price; Classes describes the behaviors of the object – in JAVA programming its called methods – public int getUpdate() {return d;}; Data and Methods can be visible only within the scope of the class, which declared them and their descendants (private / protected), or visible to all other classes (public) – Access Modifier. OOP FUNDAMENTAL: OBJECT
Objects are instances of classes or the realization of the classes.
Objects are units of abstraction. Object can communicate with other objects using messages. It can passes a message to another object, which results in the invocation of a method. Objects then perform the actions that are required to get a response from the system. Software objects are conceptually similar to real world objects. An object stores its state in fields/data, and it exposes its behaviors through its methods. Example – Car proton = new Car(); CLASS VS OBJECT CLASS VS OBJECT OOP & JAVA Class Example Object Example public class Car { public class CarApp { private String model; public static void main(String[] args) { private double cc; Car proton = new Car(); public Car() { Car nissan = new Car(); this.model = model; this.cc = cc; proton.move(); } nissan.move(); } public void move() { } System.out.println(“Vrom-vrom”) } } FOUR PRINCIPLES OF OOP
In order for a programming language to be object-
oriented, it has to enable working with classes and objects as well as the implementation and use of the fundamental object-oriented principles and concepts: Inheritance, Abstraction, Encapsulation and Polymorphism. FOUR PRINCIPLES OF OOP: INHERITANCE
Inheritance is a way to reuse a written code, and used is
it again and again. The class which is inherited is called the Base class & the class which inherits is called the Derived class. They are also called parent/super and child/sub class. Derived class inherits a base class, the derived class can use all the methods which are defined in base class, hence making code reusable. FOUR PRINCIPLES OF OOP: ENCAPSULATION The action of enclosing something in or as if in a capsule. Hide unnecessary details in the classes and provide a clear and simple interface for working with them. It describes the idea of bundling data and methods that work on that data within one unit, e.g., a class in Java. This concept is also often used to hide the internal representation, or state, of an object from the outside - This is called Information Hiding. Getter method retrieves an attribute; Setter method changes it – Implement this information-hiding mechanism by making your class data inaccessible from the outside and by providing getter and/or setter methods for data that shall be readable or updatable by other classes. FOUR PRINCIPLES OF OOP: ABSTRACTION
“An abstraction denotes the essential characteristics of an
object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer” — G. Booch. Abstraction is the process of hiding all but the relevant information about a thing to make things less complex and more efficient for the user – shows only essential characteristics. FOUR PRINCIPLES OF OOP: POLYMORPHISM
Methods with same name but different arguments,
which will perform different actions – methods with same name, but functioning in different ways. Polymorphism means one name, many forms. There are 2 basic types of polymorphism – Overloading & Overriding. ACCESS MODIFIERS OF CLASS MEMBERS
Java provides a number of access modifiers to set access
levels for classes, variables, methods, and constructors. The four access levels are:- Visible to the package, the default. No modifiers are needed. Visible to the class only – private . Visible to the world – public. Visible to the package and all subclasses – protected. ACCESS MODIFIERS OF CLASS MEMBERS
Chemistry Past Year Chemistry Past Year Chemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearphysics Pass Year 2009
Chemistry Past Year Chemistry Past Year Chemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Year
Chemistry Past Year Chemistry Past Year Chemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Yearchemistry Past Year