java chapter 3

5
Java - Chapter 3 Page 1 of 5 3. OBJECT-ORIENTED PROGRAMMING CONCEPTS Software objects are conceptually similar to real-world objects: they too consist of a state and related behavior. An object stores its state in fields (also called variables in programming languages). An object also has methods (functions in programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object- oriented programming. We now explain the common terms used in object-oriented programming: 1. Objects and Classes: An object is the basic entity in an object-oriented program. Program objects should be chosen so that they match closely with the real-world objects. An object takes space in memory and has an associated address. When a program is executed, the objects interact with each other by sending messages. Each object contains data and code to manipulate the data. Just as in C programming language we can declare variables of different data types such as int, float, char, etc., in Java, the entire set of data and code of an object can be made a user-defined data type using the concept of class. A class may be thought of as a ‘data type’ and an object as a ‘variable’ of that type. Once a class has been defined, we can create objects belonging to that class. A class is thus a collection of objects of similar type. E.g., banana, apple, grapes and mango are members of the class fruit. 2. Data Abstraction and Encapsulation: Data encapsulation is also called data hiding. The wrapping up of data and methods into a single unit called a class is known as encapsulation. The data is available only to the methods which are wrapped in the class. These methods provide an interface between the object’s data and the program. Thus, we say that the data is hidden from the program. Encapsulation makes it possible to treat objects as ‘black boxes’, each performing a specific task without any concern for the internal implementation. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Data abstraction refers to the act of representing essential features without including the background details or explanations. Abstraction means simplifying complex reality. Data and Method Information “in” Information “out”

Upload: mukesh-tekwani

Post on 19-Jan-2015

720 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Java   chapter 3

Java - Chapter 3 Page 1 of 5

3. OBJECT-ORIENTED PROGRAMMING CONCEPTS

Software objects are conceptually similar to real-world objects: they too consist of a state and related behavior. An object stores its state in fields (also called variables in programming languages). An object also has methods (functions in programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

We now explain the common terms used in object-oriented programming: 1. Objects and Classes:

An object is the basic entity in an object-oriented program. Program objects should be chosen so that they match closely with the real-world objects. An object takes space in memory and has an associated address. When a program is executed, the objects interact with each other by sending messages. Each object contains data and code to manipulate the data.

Just as in C programming language we can declare variables of different data types such as int, float, char, etc., in Java, the entire set of data and code of an object can be made a user-defined data type using the concept of class. A class may be thought of as a ‘data type’ and an object as a ‘variable’ of that type. Once a class has been defined, we can create objects belonging to that class. A class is thus a collection of objects of similar type. E.g., banana, apple, grapes and mango are members of the class fruit.

2. Data Abstraction and Encapsulation:

Data encapsulation is also called data hiding. The wrapping up of data and methods into a single unit called a class is known as encapsulation. The data is available only to the methods which are wrapped in the class. These methods provide an interface between the object’s data and the program. Thus, we say that the data is hidden from the program. Encapsulation makes it possible to treat objects as ‘black boxes’, each performing a specific task without any concern for the internal implementation. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.

Data abstraction refers to the act of representing essential features without including the background details or explanations. Abstraction means simplifying complex reality.

Data and

Method

Information “in” Information “out”

Page 2: Java   chapter 3

The Java Programming Language

Page 2 of 5 Java - Chapter 3

Difference between data abstraction and data encapsulation:

• Representation of essential feature without including the background detail is called data abstraction while collection of all data and method into a single unit called encapsulation.

• In data abstraction we were ignore about the details regarding an object type while encapsulation describe details information about an object type.

What are the advantages of bundling code into objects? 1. Modularity:- The source code for an object can be written and maintained independently

of the source code for other objects. Once created, an object can be easily reused. 2. Information-hiding: By interacting only with an object's methods, the details of its

internal implementation remain hidden from the outside world. 3. Code re-use: If an object already exists (perhaps written by another software developer),

you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.

4. Pluggability and debugging ease: If a particular object turns out to be problematic, you

can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

3. Inheritance Inheritance is the process by which objects of one class acquire the properties of objects of another class. Defining new classes from the existing one is called inheritance.. The new class will get all the methods and properties of the existing class. The new class is known as the sub class / child class / derived class. The existing class is known as the super class, parent class / base class. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Inheritance is implied by “is-a” relationship.

In OOP, the concept of inheritance provides the idea of reusability. That is, we can add features to an existing class without modifying it. We derive a new class from an already existing class. The new class has the features of the old class as well.

For example, helicopter is a part of the class aircraft. This class aircraft can have other subdivisions such as passenger aircraft, cargo plane, etc. In real life a manager is an employee. So in OOPL, a manager class is inherited from the employee class.

4. Polymorphism

Polymorphism means the ability to take more than one form. A mathematical operation may have different behaviour in different instances. This behaviour depends upon the type of data used in the operation. E.g., consider the operation of addition (+). When applied on two numbers, we get the sum of the two numbers (3 + 4 = 7). But when the same operator is

Page 3: Java   chapter 3

The Java Programming Language

Prof. Mukesh N Tekwani Page 3 of 5

applied on strings, it produces a third string by concatenation (e.g., “INTER” + “NET” gives “INTERNET”).

Polymorphism allows objects having different internal structures to share the same external interface.

5. Dynamic Binding

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at runtime.

The concept of dynamic binding can be understood as follows: In strongly-typed programming languages such as C, we must declare variable before they are used. E.g., in C, we declare int k. This statement also defines the memory space for the variable k (in this case 2 bytes). With this declaration we bind the name k to the type integer. This enables the compiler to check for data type consistency at compile time. If we wrote k = ‘MUMBAI’ it will result in a data-type mismatch error. This type of binding is called “static binding” because it is fixed at compile time.

Consider a variable N. If the type of variable is implicitly associated by its contents, we say that N is dynamically bound to a data type T. This associative process is called dynamic binding.

Consider the following example which is only possible with dynamic binding:

if somecondition() == TRUE then n := 123 else n := 'abc' endif

The type of n after the if statement depends on the evaluation of somecondition(). If it is TRUE, n is of type integer whereas in the other case it is of type string.

6. Message Communication

A message is a request to an object to invoke (execute) one of its methods. A message therefore contains

• the name of the method and • the arguments of the method.

In an object-oriented program, objects communicate with one another by sending and receiving information. When an object receives a message, one of its method (or procedure / function) is executed.

Page 4: Java   chapter 3

The Java Programming Language

Page 4 of 5 Java - Chapter 3

Message passing involves specifying the name of the object, the name of the method and the information to be sent. E.g., consider the statement:

Employee.salary(name);

Here, Employee is the object, salary is the message (method) and name is the parameter that contains information. What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface", or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.

Questions and Exercises:

Objective Questions

1. Real-world objects contain ___ and ___.

2. A software object's state is stored in ___.

3. A software object's behavior is exposed through ___.

4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ___.

5. A blueprint for a software object is called a ___.

6. Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword.

7. A collection of methods with no implementation is called an ___.

8. A namespace that organizes classes and interfaces by functionality is called a ___.

9. The term API stands for ___

Page 5: Java   chapter 3

The Java Programming Language

Prof. Mukesh N Tekwani Page 5 of 5

QUESTIONS

1. What is object-oriented programming? How does it differ from procedure-oriented programming? (Net-exercise)

2. How are data and methods organized in an object-oriented program?

3. What is an object? What are the advantages of bundling code into objects?

4. Distinguish between:

a. Classes and objects

b. Data encapsulation and data abstraction

c. Inheritance and polymorphism

d. Static and Dynamic binding

5. Define the terms inheritance and package.

6. What is message passing?

Answers to Questions

1. state and behavior.

2. fields.

3. methods.

4. encapsulation.

5. class.

6. superclass, subclass, extends.

7. interface.

8. package.

9. Application Programming Interface.