Unit 1 Basics of OOP Basics of Object Oriented Programming (OOP) Need For OOP Paradigm
This document provides an overview of object-oriented programming (OOP) concepts including:
- The need for OOP due to increasing complexity in procedural programming. OOP organizes code around objects and data.
- Key characteristics of OOP include emphasis on data, modularity through classes and methods, encapsulation, inheritance and polymorphism.
- Core OOP concepts like classes, objects, methods, inheritance and exceptions are explained at a high level. Classes provide blueprints for objects and inheritance allows subclasses to inherit attributes. Exceptions represent errors and abnormal conditions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
81 views14 pages
Unit 1 Basics of OOP Basics of Object Oriented Programming (OOP) Need For OOP Paradigm
This document provides an overview of object-oriented programming (OOP) concepts including:
- The need for OOP due to increasing complexity in procedural programming. OOP organizes code around objects and data.
- Key characteristics of OOP include emphasis on data, modularity through classes and methods, encapsulation, inheritance and polymorphism.
- Core OOP concepts like classes, objects, methods, inheritance and exceptions are explained at a high level. Classes provide blueprints for objects and inheritance allows subclasses to inherit attributes. Exceptions represent errors and abnormal conditions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14
UNIT 1 Basics of OOP
Basics of Object Oriented Programming (OOP)
Need for OOP paradigm: Two Paradigms of Programming: As you know, all computer programs consist of two elements: code and data. Furthermore, a program can be conceptually organized around its code or around its data. That is, some programs are written around what is happening and others are written around who is being affected. These are the two paradigms that govern how a program is constructed. The first way is called the processoriented model. This approach characterizes a program as a series of linear steps !that is, code". The processoriented model can be thought of as code acting on data. Procedural languages such as # employ this model to considerable success. Problems with this approach appear as programs grow larger and more comple$. To manage increasing comple$ity, the second approach, called ob%ectoriented programming, was conceived. &b%ectoriented programming organizes a program around its data !that is, ob%ects" and a set of welldefined interfaces to that data. An ob%ectoriented program can be characterized as data controlling access to code. As you will see, by switching the controlling entity to data, you can achieve several organizational benefits. Procedure oriented Programming: (POP) 'n this approach, the problem is always considered as a se(uence of tasks to be done. A number of functions are written to accomplish these tasks. )ere primary focus on Functions and little attention on data. There are many high level languages like #&*&+, F&,T,A-, PA.#A+, # used for conventional programming commonly known as P&P. P&P basically consists of writing a list of instructions for the computer to follow, and organizing these instructions into groups known as functions. OOP through Jaa N!"# "$! %ept& 1 UNIT 1 Basics of OOP A typical P&P structure is shown in below: -ormally a flowchart is used to organize these actions and represent the flow of control logically se(uential flow from one to another. 'n a multifunction program, many important data items are placed as global so that they may be accessed by all the functions. /ach function may have its own local data. 0lobal data are more vulnerable to an in advent change by a function. 'n a large program it is very difficult to identify what data is used by which function. 'n case we need to revise an e$ternal data structure, we should also revise all the functions that access the data. This provides an opportunity for bugs to creep in. 1rawback: 't does not model real world problems very well, because functions are action oriented and do not really corresponding to the elements of the problem. "haracteristics of POP: /mphasis is on doing actions. +arge programs are divided into smaller programs known as functions. 2ost of the functions shared global data. 1ata move openly around the program from function to function. Functions transform data from one form to another. /mploys topdown approach in program design. OOP through Jaa N!"# "$! %ept& 2 UNIT 1 Basics of OOP OOP: &&P allows us to decompose a problem into a number of entities called ob%ects and then builds data and methods around these entities. 1/F: &&P is an approach that provides a way of modularizing programs by creating portioned memory area for both data and methods that can used as templates for creating copies of such modules on demand. OOP "haracteristics: /mphasis on data. Programs are divided into what are known as methods. 1ata structures are designed such that they characterize the ob%ects. 2ethods that operate on the data of an ob%ect are tied together. 1ata is hidden. &b%ects can communicate with each other through methods. ,eusability. Follows bottomup approach in program design. Organi'ation of OOP: OOP through Jaa N!"# "$! %ept& 3 UNIT 1 Basics of OOP ( )a* of ie)ing )or+d: (gents: &&P uses an approach of treating a real world agent as an ob%ect. &b%ectoriented programming organizes a program around its data !that is, ob%ects" and a set of welldefined interfaces to that data. An ob%ectoriented program can be characterized as data controlling access to code by switching the controlling entity to data. ,esponsibi+it*: Primary motivation is the need for a platformindependent !that is, architecture neutral" language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. &b%ects with clear responsibilities. /ach class should have a clear responsibility. 'f you can state the purpose of a class in a single, clear sentence, then perhaps your class structure needs some thought. -essages: 3e all like to use programs that let us know what going on. OOP through Jaa N!"# "$! %ept& 4 UNIT 1 Basics of OOP Programs that keep us informed often do so by displaying status and error messages. These messages need to be translated. .o they can be understood by end users around the world. -ethods: A method is a group of instructions that is given a name and can be called up at any point in a program simply by (uoting that name. The idea of methods appears in all programming languages, although sometimes it goes under the name functions and sometimes under the name procedures. /verything in 4ava is enclosed within a class .so the functions within it are called methods ".($$!$ and IN$T(N"!$: #+A../.: #lass is an idea of an &b%ect. From &ne class any number of 'nstances can be created. 't is an encapsulation of attributes and methods. #lass is a blueprint for an ob%ect. .ynta$ of #+A..: class 5#lass-ame6 7 88Attributes8variables 88#onstructors 88methods 9 '-.TA-#/: 'nstance is an &b%ect of a class which is an entity with its own attribute values and methods. #reating an 'nstance #lass-ame ref:ariable; ref:ariable < new #onstructor!"; OOP through Jaa N!"# "$! %ept& 5 UNIT 1 Basics of OOP !or" #lass-ame ref:ariable < new #onstructor!"; Jaa "+ass /ierarch*: 'n 4ava, class &b%ect is the base class to all other classes if we do not e$plicitly say e$tends in a new class definition, it implicitly e$tends &b%ect. The tree of classes that e$tend from &b%ect and all of its subclasses are called the class hierarchy. All classes eventually lead back up to ob%ect. This will enable consistent access of ob%ects of different classes. Inheritance: 'nheritance is the process by which ob%ects of one class ac(uire the properties of ob%ects of another class. 'nheritance supports the concept of hierarchical classification. A deeply inherited subclass inherits all of the attributes from each of its ancestors in the class hierarchy. 2ost people naturally view the world as made up of ob%ects that are related to each other in a hierarchical way. 'nheritance: A new class !subclass, child class" is derived from the e$isting class !base class, parent class". 2ain uses of 'nheritance: =. ,eusability >. Abstraction -ethod Binding: &b%ects are used to call methods. OOP through Jaa N!"# "$! %ept& 6 UNIT 1 Basics of OOP 2ethod *inding is an ob%ect that can be used to call an arbitrary public method, on an instance that is ac(uired by evaluating the leading portion of a method binding e$pression via a value binding. 't is legal for a class to have two or more methods with the same name. 4ava has to be able to uni(uely associate the invocation of a method with its definition relying on the number and types of arguments. Therefore the samenamed methods must be distinguished: =" by the number of arguments, or >" by the types of arguments overloading and inheritance are two ways to implement polymorphism. -ethod Oerriding: There may be some occasions when we want an ob%ect to respond to the same method but have different behavior when that method is called. That means, we should override the method defined in the superclass. This is possible by defining a method in a sub class that has the same name, same arguments and same return type as a method in the superclass. Then when that method is called, the method defined in the sub class is invoked and e$ecuted instead of the one in the superclass. This is known as overriding. !0ceptions in Jaa: /$ception is an abnormal condition that arises in the code se(uence. /$ceptions occur during compile time or run time. throwable is the super class in e$ception hierarchy. #ompile time errors occur due to incorrect synta$. ,untime errors happen when ?ser enters incorrect input ,esource is not available !e$. file" +ogic error !bug" that was not fi$ed. /$ception classes 'n 4ava: OOP through Jaa N!"# "$! %ept& 7 UNIT 1 Basics of OOP /$ceptions are ob%ects. 3hen you throw an e$ception, you throw an ob%ect. @ou can throw %ust any ob%ect as an e$ception, however, only those ob%ects whose classes descend from Throwable. Throwable serves as the base class for an entire family of classes, declared in %ava.lang, that your program can instantiate and throw. Throwable has two direct subclasses, /$ception and /rror. /$ceptions are thrown to signal abnormal conditions that can often be handled by some catcher through it. 'f they may not be caught, this could result in a dead thread. /rrors are usually thrown for more serious problems, such as &ut&f2emory/rror, that may not be so easy to handle. 'n general, code you write should throw only e$ceptions, not errors. /rrors are usually thrown by the methods of the 4ava AP', or by the 4ava virtual machine itself. $ummar* of OOP "oncepts: 1ata abstraction /ncapsulation 'nheritance Polymorphism =. 1ata Abstraction: )iding unnecessary data from user is called Abstraction. Access specifiers like public, private etc. are used to provide different levels in abstraction. /$ample: .uppose take a car: 'n that we will have breaks and steering and etc... )ere when we are changing the gear bo$ to up or bottom then gears will work but we donAt know how they are managing internally and we donAt no how it is working. As a driver we donAt need to know how it is working internally when we are changing every time. That is the reason they will hide the wires and cables internally and they will show up only gear bo$ to us. OOP through Jaa N!"# "$! %ept& 8 UNIT 1 Basics of OOP Advantages: #ode will be clean and for managing it will be very easy >. /ncapsulation: /ncapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. &ne way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a welldefined interface. 'n 4ava the basis of encapsulation is the class. A class defines the structure and behavior !data and code" that will be shared by a set of ob%ects. /ach ob%ect of a given class contains the structure and behavior defined by the class, as if it were stamped out by a mold in the shape of the class. For this reason, ob%ects are sometimes referred to as instances of a class. Thus, a class is a logical construct; an ob%ect has physical reality. Advantage: 2aintenance will be good /$ample: 2edical #apsules i.e. one drug is stored in bottom layer and another drug is stored in ?pper layer these two layers are combined in single capsule. B. 'nheritance: 'nheritance: A class inherits state and behavior from its superclass. 'nheritance provides a powerful and natural mechanism for organizing and structuring software programs. #reating a new class from e$isting class is called 'nheritance. ,eusability is main advantage in inheritance. /$ample: Father and .on ,elationship C. Polymorphism: 'f single method performs different task is called polymorphism. Polymorphism !many forms" is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the e$act nature of the situation. Polymorphism means many forms. 't is divided into >types =" #ompile time polymorphism OOP through Jaa N!"# "$! %ept& 9 UNIT 1 Basics of OOP >" ,un time polymorphism 2ethod overloading is an e$ample for #ompile time polymorphism and 2ethod overriding is an e$ample for ,un time polymorphism. /$ample:Person !Person in )ome act is husband8son, in &ffice acts /mployer. in Public 0ood #itizen. (dantages of OOPs: 3e can eliminate redundant code and e$tend the use of classes with the concept of inheritance. 3e can build the programs from the standard working modules that communicate with one another, rather than having to start writing the code from beginning. This leads to saving of development time and higher productivity. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. 't is possible to have multiple instance of an ob%ect to e$ist without any interference. .oftware comple$ity can be managed. && systems can be easily upgraded from small to large systems. (pp+ications of OOPs: ,eal time systems .imulation and modeling && database )yperte$t, hypermedia and e$pertte$t A' and e$pert systems -eural networks and parallel programming OOP through Jaa N!"# "$! %ept& 10 UNIT 1 Basics of OOP 1ecision support and office automation systems #A28#A1 systems "onc+usion: /verything is an ob%ect. 2ethods are used to pass the messages between ob%ects. &b%ects are packaged by class. 2ethods and variables are considered as the members of class. &ne class can inherit the many classes &ne method can do many actions. #omple$ity is managed by abstraction and composition. 3e can hide and bind the data. &&P follows the bottomup approach. "oping )ith comp+e0it*: The size and comple$ity of a small program is small and simple whereas, the size and comple$ity of a large application program is large and hard. The comple$ity in dealing with the problems to build a large application depends on the composition and abstraction mechanisms. #omposition mechanisms: 3e can create ob%ects of another !super" class as instance variable in the current !sub" class. This capability is called composition. #omposition is one form of software reuse in which a class has references to ob%ects of other classes as members. 't is called hasa relationship. class Figure 7 double dim=; OOP through Jaa N!"# "$! %ept& 11 UNIT 1 Basics of OOP double dim>; Figure!double a,double b" 7 dim=<a; dim><b; 9 double area!" 7 .ystem.out.println!DArea for figure is undefinedD"; return E; 9 9 class ,ectangle e$tends Figure 7 ,ectangle!double a,double b" 7 super!a,b"; 9 double area!" 7 .ystem.out.println!D'nside area for rectangleD"; return dim=Fdim>; 9 9 class Triangle e$tends Figure 7 Triangle!double a,double b" 7 super!a,b"; 9 double area!" 7 .ystem.out.println!D'nside area for triangleD"; return dim=Fdim>8>; 9 9 class FindAreas 7 public static void main!.tring argsGH" 7 Figure f<new Figure!=E,=E"; ,ectangle r<new ,ectangle!I,J"; Triangle t<new Triangle!=E,K"; 88creating ob%ects of one!super" class in another!sub" class:#omposition
OOP through Jaa N!"# "$! %ept& 12 Figure figref; figref<f; .ystem.out.println!Darea isDLfigref.area!""; UNIT 1 Basics of OOP
figref<r; .ystem.out.println!Darea isDLfigref.area!""; figref<t; .ystem.out.println!Darea isDLfigref.area!""; 9 9 &?TP?T: 'nside area for rectangle area is CJ 'nside area for triangle area is CE 'nside area for figure is undefined area is E (bstraction -echanisms: Abstraction mechanism provides a way to manage the system components and their corresponding interactions without considering the details of building those components. The definition of abstraction can also be given as elimination of irrelevant information and amplification of essential information. Abstraction is nothing but finding out what is common among different things. %ata (bstraction: From the generic point of view, process of ignoring complete information of a particular entity and knowing only its essential information is called data abstraction. 1ata Abstractions are used for organizing the data. /.g.: -ame !string"; 2arks !num"; 0rade !char"; .tudent -umber !num"; .tudent Type Procedural Abstraction8Functional Abstraction: procedural abstraction enables the programmers to know only significant details of a function or a procedure. For e$: how to call a procedure, what is the functionally of the procedure etc. Behaiora+ (bstraction: *ehavioral Abstractions are used to combine procedural and data abstractions. OOP through Jaa N!"# "$! %ept& 13 UNIT 1 Basics of OOP /.g.: 1ata .tate /n(ueue 's Full 's /mpty 1e(ueue 'nitialize Mueue &b%ect OOP through Jaa N!"# "$! %ept& 14