They know who you are. Show them what you can be.
--Your friends at LectureNotes

Note for Object Oriented Programming Using JAVA - OOPJ By Ranjit Kumar

  • Object Oriented Programming Using JAVA - OOPJ
  • Note
  • West Bengal University of technology - WBUT
  • 1 Topics
  • 111 Offline Downloads
  • Uploaded
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-2

“account” are two objects in a program, then the customer object may send a message to the account object requesting for the bank balance. Each object contains data, and code to manipulate the data. Objects can interact without having to know details of each other’s data or code. It is sufficient to know the type of message accepted, and the type of response returned by the objects. Although different authors represent them differently. Fig. Shows two notations that are popularly used in objectoriented analysis and design. Object: STUDENT DATA Name Date-f-birth Marks FUNCTIONS Total Average create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement Fruit mango; will create an object mango belonging to the class fruit. Data Abstraction and Encapsulation The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions, which are wrapped in the class, can access it. These functions provide the interface between the object’s data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding. Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and functions to operate on these attributes. They encapsulate all the essential properties of the objects that are to be created. The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member functions. Since the classes use the concept of data abstraction, they are known as Abstract Data Types (ADT) Display Two Ways of Representing an Object STUDENT Total Average Display Classes Objects contain data, and code to manipulate that data. The entire set of data and code of an object can be made a userdefined data type with the help of a class. In fact, objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class. Each object is associated with the data of type class with which they are created. Inheritance Inheritance is the process by which objects of one class acquire the properties of objects of another class. For example, the bird ‘robin’ is a part of the class “flying bird’ which is again a part of the class ‘bird’. The prin-ciple behind this sort of division is that each derived class shares common characteristics with the class from which it is derived as illustrated in Fig. Concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. The real appeal and power of the inheritance mechanism is that it allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class in such a way that it does not introduce any undesirable side-effects into the rest of the classes. Note that each sub-class defines only those features that are unique to it. Without the use of classification, each class would have to explicitly include all of its features. Definition A class is a collection of objects of similar type. For e.g., mango, apple and orange are members of the class fruit. Classes are user-defined data types and behave like the built-in types of a programming language. The syntax used to 2

Text from page-3

A function call associated with a polymorphism reference depends on the dynamic type of that reference. Bird E.g. Consider the procedure “draw” in Fig. By inheritance, every object will have this procedure. Its algorithm is, however, unique to each object and so the draw procedure will be redefined in each class that defines the object. At run-time, the code matching the object under current reference will be caned. Attributes Feathers Lay eggs Flying Bird Attributes --------------- Robin Attributes ------------ Non-Flying Bird Attributes --------------- Swallow Attributes ------------ Penguin Attributes ------------ Kiwi Attributes ------------ Message Passing An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, therefore, involves the following basic steps: 1. Creating classes that define objects and their behavior, 2. Creating objects from class definitions, and 3. Establishing communication among objects. Fig. Property inheritance Polymorphism Polymorphism, means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation. For e.g., consider the operation of addition of two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Figure below illustrates that a single function name can be used to handle different number and different types of arguments. This is something similar to a particular word having several different meanings depending on the context. Using a single function name to perform different types of tasks is known as function overloading. Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ. Polymorphism is extensively used in implementing inheritance. Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. The concept of message passing makes it easier to talk about building systems that directly model or simulate their real-world counterparts. A message for an object is a request for execution of a procedure, and therefore will invoke a function (procedure) in the receiving object that generates the desired result. Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent. Objects have a life cycle. They can be created and destroyed. Communication with an object is feasible as long as it is a’1ive. Benefits of oop OOP offers several benefits to both the program designer and the user. Object-orientation contributes to the solution of many problems associated with the development and quality of software products. The new technology promises greater programmer productivity, better quality of software and lesser maintenance cost. The principal advantages are: Through inheritance, we can eliminate redundant code and extend the use of existing classes. • We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. 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. • It is possible to have multiple instances of an object to coexist without any interference. It is possible to map objects in the problem domain to those in the program. Shape Draw () Circle object Draw (circle) Box object Draw (box) Triangle object Draw (triangle) Fig. Polymorphism Dynamic Binding Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance. • • It is easy to partition the work in a, project based on objects. • The data-centered design approach enables us to capture more details of a model in implementable form. • Object-oriented systems can be easily upgraded from small to large systems. 3

Text from page-4

• Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler. programming, up to now, has been in the area of user interface design such as windows. Hundreds of windowing systems have been developed, using the OOP techniques. • Software complexity can be easily managed. Real-business systems are often much more complex and contain many more objects with complicated attributes and methods. OOP is useful in these types of applications because it can simplify a complex problem. The promising areas for application of OOP include: While it is possible to incorporate all these features in an objectoriented system, their importance depends on the type of the project and the preference of the programmer. There are a number of issues that need to be tackled to reap some of the benefits stated above. For instance, object libraries must be available for reuse. The technology is still developing and, current products may be superseded quickly. Strict controls and protocols. need to be developed if reuse is not to be compromised. • Real-time systems • Simulation and modeling • • Object-oriented databases Hypertext, hypermedia and expertext Developing software that is easy to use makes it hard to build. It is hoped that the object-. oriented programming tools would help manage this problem. • AI and expert systems • Neural networks and Parallel programming Decision support and office automation systems Object Oriented Languages Object-oriented programming is not the right of any particular language. Like structured. • CIM/CAM/CAD systems Programming, OOP concepts can be implemented using languages such as C and Pascal. However, programming becomes clumsy and may generate confusion when the programs grow large. A language that is specially designed to support the OOP concepts makes it easier to implement them. The languages should support several of the OOP concepts to claim that they are object., oriented. Depending upon the features they support, they can be classified into the following two categories: 1. Object-based programming languages, and 2. Object-oriented programming languages. Object-based programming is the style of programming that primarily supports encapsulation and object identity. Major features that are required for object-based programming are: • Data encapsulation • Data hiding and access mechanisms • Automatic initialization and clear-up of objects • Operator overloading Languages that support programming with objects are said to be object-based programming languages. They do not support inheritance and dynamic binding. Ada is a typical object-based programming language. Object-oriented programming incorporates all of object-based programming features along with two additional features, namely, inheritance and dynamic binding. Object-oriented programming can therefore be characterized by the following statement: Object-based features + inheritance + dynamic binding Languages that support these features include C++, Smalltalk, Object Pascal and Java. There are a large number of object-based and object-oriented programming languages. Applications of Oop Applications of OOP are beginning to gain importance in many areas. The most popular application of object-oriented Object-oriented technology is certainly going to change the way the software engineers think, analyze, design and implement future systems. Points to Ponder Object-oriented programming as an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as tempalates for creating copies of such modules on demand.” • • A class is a collection of objects of similar type Inheritance is the process by which objects of one class acquire the properties of objects of another class. • Polymorphism, means the ability to take more than one form. • Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time • Object-orientation contributes to the solution of many problems associated with the development and quality of software products • Object-oriented programming incorporates all of objectbased programming features along with two additional features, namely, inheritance and dynamic binding • The most popular application of object-oriented programming, up to now, has been in the area of user interface design such as windows Questions 1. Discuss features of OOPS 2. What do you mean by Dynamic Binding? How it is useful in OOPS 3. What are the Applications of OOPS? Reference • Object Oriented Programming With C++ By E Balagurusamy • Let US C++ By Yashwant Kanetkar. 4

Text from page-5

1: Introduction to objects Why has object-oriented programming had such a sweeping impact on the software development community? Object-oriented programming appeals at multiple levels. For managers, it promises faster and cheaper development and maintenance. For analysts and designers, the modeling process becomes simpler and produces a clear, manageable design. For programmers, the elegance and clarity of the object model and the power of objectoriented tools and libraries makes programming a much more pleasant task, and programmers experience an increase in productivity. Everybody wins, it would seem. If there’s a downside, it is the expense of the learning curve. Thinking in objects is a dramatic departure from thinking procedurally, and the process of designing objects is much more challenging than procedural design, especially if you’re trying to create reusable objects. In the past, a novice practitioner of object-oriented programming was faced with a choice between two daunting tasks: 1. Choose a language such as Smalltalk in which you had to learn a large library before becoming productive. 2. Choose C++ with virtually no libraries at all, and struggle through the depths of the language in order to write your own libraries of objects. It is, in fact, difficult to design objects well – for that matter, it’s hard to design anything well. But the intent is that a relatively few experts design the best objects for others to consume. Successful OOP languages incorporate not just language syntax and a compiler, but an entire development environment including a significant library of well-designed, easy to use objects. Thus, the primary job of most programmers is to use existing objects to solve their application problems. The goal of this chapter is to show you what object-oriented programming is and how simple it can be. This chapter will introduce many of the ideas of Java and object-oriented programming on a conceptual level, but keep in mind that you’re not expected to be able to write full-fledged Java programs after reading this chapter. All the detailed descriptions and examples will follow throughout the course of this book. The progress of abstraction All programming languages provide abstractions. It can be argued that the complexity of the problems you can solve is directly related to the kind and quality of abstraction. By “kind” I mean: what is it that you are abstracting? Assembly language is a small abstraction of the underlying machine. Many so-called “imperative” 1

Lecture Notes