You are on page 1of 9

Java Frequently Asked Questions (FAQs)

(1.Introduction to OOP)
1. What is procedure-oriented programming? Procedure-oriented programming is a programming paradigm or programming approach by which a program can be written as a collection of procedures or functions. In this programming approach, there is always a controlling procedure that is also called as main procedure. From the main procedure, a procedure call is used to call the required procedure. After the execution of sequence of instructions in the called procedure is completed, the flow of control continues from where the call was made. The main procedure co-ordinates calls to procedures and hands over appropriate data as parameters. The data is processed by the procedures and once the program has finished, the result is displayed. Main Procedure

Procedure-1

Procedure-2

Procedure-3

2. What is object-oriented programming? Object-oriented programming is a programming paradigm or programming approach by which a program can be written as a collection of interacting objects. In this programming approach, the program is divided into objects. The data is compartmentalized or encapsulated with the associated functions (that operate on it) and this compartment or capsule is called as an object. Thus, an object is visualized as a combination of data and functions which manipulate them. During the execution of a program, the objects interact with each other by sending messages and receiving responses. E.g., in a program that performs withdrawals from an account, a customer object can send a withdrawal message to a bank account object. An object communicating with other objects need not be aware of the internal working of the objects with which it interacts. This situation is analogous to operating a television remote controller where one need not know internal operation. The objects internal structure is totally hidden from the user and this concept is called as data/information hiding or data encapsulation.
Data Object A

Function

Data

Data

Function

Function

Object B

Object C

3. What are the differences between procedure-oriented programming and object-oriented programming? Procedure-oriented programming 1) Procedure-oriented programming is a 1) Object-oriented programming Object-oriented programming is a

programming paradigm by which a program can be written as a collection of procedures. 2) Emphasis is on procedures. In other words, importance is given to doing things.

programming paradigm by which a program can be written as a collection of interacting objects. 2) Emphasis is is on given data. to In data other rather words, than

importance procedures.

3) Data can be moved very freely around the system from function to function. Hence, there is no data hiding.

3) Here data can not be moved freely around the system. Data can be protected at different levels. E.g., public data can be accessed by the same class and other classes. Where as private data can not be accessed by other classes.

4) It employs the top-down approach in program design. i.e., first the main procedure is designed. Later other procedures that interact with main procedure will be designed. 5) In this programming approach, data and

4)

It

employs design.

the i.e.,

bottom-up first the

approach objects

in are

program

designed. Later these are combined to achieve desired objective. 5) In this programming approach, data and functions that operate on data are combined into a single unit.

functions are treated as separate.

6) Complexity rises in developing a large software system. Ex: C, PASCAL, ALGOL

6) Software complexity can easily be reduced.

Ex: C++, Java

4. What are various benefits of OOP? (or) How the OOP improve software development? 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 principal advantages are: 1. Software complexity can be easily managed. 2. It is easy to partition the work in a project based on objects. 3. Information hiding and data abstraction increase reliability. 4. Through inheritance, we can eliminate redundant code and extend the use of existing classes. Thus, it provides reusability. 5. Object- oriented systems can be easily upgraded from small to large systems. 6. Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler.

Java FAQs-Introduction to OOP

7. 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. 5. What are various concepts of OOP?

Object: An identifiable Data The abstraction: process of Data Encapsulation: The process data of and thing that has state

and exhibits behavior.

representing essential an object. details of

combining

functions into a single unit. Class: objects A collection that of

share

Composition: existing

assembling rather

common characteristics.

components

than crafting new ones.

Concepts of Object-oriented programming

Polymorphism: Inheritance: The

The

ability of an object to exhibit behaviors times. at different different process of creating one or more classes from

existing class(es).

Message

Communication:

Dynamic

Binding:

The

The process in which an object calls its function by sending

process of linking a function call to code associated with it at run-time.

optionally arguments to it.

Java FAQs-Introduction to OOP

6. What is an object? An Object is an identifiable entity or thing that has state and exhibits behavior. From this definition, it is clear that an object has the following three characteristics: 1. Each object should have an identity: An objects identity is used to identify it uniquely in the world of objects. Two objects may exhibit the same behavior, may or may not have the same state, but never have the same identity. 2. Each object has state: An objects state is collection of attributes or data that it possesses. 3. Each object exhibits behavior: An objects behavior is collection of functions that it possesses. Each function, in turn, is used to specify the operation the object performs. Usually, an objects behavior refers to the changes that occur to its attributes over a period of time. Ex:

From these examples, it is clear that an object represents real world entity such as a person, a vehicle or a programming entity such as memory location. An object can be viewed in different ways. E.g., a piece of chalk, an object, can be viewed differently. A teacher treats that object as a tool used to write on black board. A manufacturer treats that object as small clay that is burnt. Each object has its own life cycle; it can be constructed, utilized and destroyed. 7. What is a class?

A class is collection of objects that share common characteristics. Suppose that there are three objects: car, bus and scooter. Each of these is a vehicle. Each of these contains the common properties such as number of wheels and mileage. Each of these can be used to travel. By using these common characteristics, a class namely Vehicle can be created. A class is an object-oriented programming concept that can be used to create user-defined data types, according to the needs of programmer. Once the user-defined data type is defined, the variables of that type, usually called as objects, can be created as and when they are needed.

Ex:

int a,b,c;

//int is pre-defined data type and a,b and c are variables of type int. 4

Java FAQs-Introduction to OOP

Similarly, class Vehicle { //fields and methods } Vehicle car=new Vehicle(); Vehicle bus=new Vehicle(); Vehicle scooter=new Vehicle(); //Vehicle is user-defined data type. //car,bus and scooter are variables of type vehicle From this example, it is clear that a class provides description for a number of similar objects. It specifies what data and what functions will be included in objects of that class. 8. What is the relationship between a class and an object? A class is collection of objects with similar properties. Object is an instance of that class. A class is used to create user-defined data types. An object is a variable of that type defined by the class. A class provides the design (or blue print or model). An object is the creation from that design. Multiple objects can be created from one design. 9. What is abstraction? Abstraction is the process of representing the essential information by leaving out unwanted information about an object to outside world. An abstraction tells users every thing they need to know about an object but nothing else. For example, on an ATM, the abstraction is formed by the buttons, cash dispenser, card in-taker and screen to display; users dont have to know functionality of internal components such as validation of PIN, dispensing cash according to given inputetc. Classes use the concept of abstraction and define a list of abstract attributes as well as functions to operate on these attributes. These attributes are called as fields and they hold data. The functions that operate on these attributes are called as methods. As class uses the concept of abstraction and this is also called as an Abstract Data Type (ADT). The abstraction of an object should be carried out by keeping user in mind; abstraction should be user-centric. 10. What is encapsulation? Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. In other words, encapsulation prevents access to non-essential details of a class. For example, when we switch on a television set, it displays the programs being telecast in the form of audio and video. However, we can not see the actual complete process of the television, such as how the signals from the cable are being transformed into audio and video. Encapsulation is also referred to as data hiding, since an object hides important and classified data from accidental manipulation by members of the same program. However, an object provides public interface to the outside world so that other objects can communicate with it.

Java FAQs-Introduction to OOP

11. What is the difference between abstraction and encapsulation? Abstraction represents the essential details of an object in user point of view. In other words, it gives specification. Encapsulation is hiding non-essential details of an object in user point of view. In other words, it hides the implementation. 12. What is inheritance? Inheritance is the process of creating new class(es) from existing class(es). Inheritance allows the developers to make one class, a kind-of another class. In an inheritance relationship, the new class is called the sub class and the original class from which new class is being derived is called the super class. All the fields and methods that belong to super class automatically become part of the sub class. Of course, the sub class can also contain its own fields and methods. Inheritance allows the programmer to classify the classes in the model of

generalization/specialization. It gives the class hierarchy. E.g., there are 3 classes namely: Crow, Sparrow and Peacock. All of these classes can become special instances of the general class: Bird.
Bird Bird classs fields Bird Classs methods Base class (or) Super class (or) Parent class

Is-A (or) is derived from

Crow Bird classs fields Crow classs fields

Sparrow Bird classs fields Sparrow classs fields

Peacock Bird classs fields Peacock classs fields

Bird Classs methods Crow classs methods

Bird Classs methods Sparrow classs methods

Bird Classs methods Peacock classs methods

Derived class (or) Sub class (or) child class

From this class hierarchy it is clear that each derived class contains its base classs content and its own content. 13.What are the advantages and disadvantages of Inheritance? Advantages of inheritance: 1. Reusability: Inheritance 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. 2. Reduction in coding: Inheritance can reduce the amount of coding by letting any sub class use the super classs functionality when it needs to, thus simplifying implementation of similar classes. In

Java FAQs-Introduction to OOP

addition, the sub class is compatible with the super class and can be used in place of super class. This allows a system to override any of super class functionality it wants and use the new objects where the super class would be used, thus adapting it to its own needs. 3. Reduction in development time: when a software system is constructed largely out of reusable components or off-the-shelf components (COTS), development time is reduced. Drawbacks of inheritance: 1. Though the sub class objects may not require all the functionalities of a super class, the sub class implements all the methods of super class. This increases the amount of memory required for execution of the program. 2. A user must know the changes in the methods of super class before he uses the sub class objects. 3. When there are common functionalities in super classes, then there is an execution overhead for the sub class when compared to having all the functionalities in a single class itself. 14.Define composition? Composition is the way to combine simple objects into more complex ones. Composited (composed) objects are often referred to as having a "has a" relationship. A real-world example of composition may be seen in an automobile: the objects wheel, steering wheel, seat, gearbox and engine may have no functionality by themselves, but an object called automobile containing all of those objects would serve a higher function, greater than the sum of its parts. 15.What is the difference between inheritance and composition? Inheritance supports the is-a relationship between two objects. E.g., the relation between two classes vehicle and car is given as car is a vehicle through inheritance. Composition supports the has-a relationship among objects. The relation among the classes automobile, wheel, steering wheel, seat, gearbox and engine is formed as automobile is composed of (or has) wheel, steering wheel, seat, gearbox and engine through composition. However, it is important to note that both of these have the common advantage: reusability. 16.What is polymorphism? Poly= Many Morpho=Form Polymorphism= ability to take many forms.

Polymorphism is the ability of an object to exhibit different behaviors.

An operation may be performed differently in different situations. E.g., the operation addition will generate the sum, if two operands for it are numbers. The same operation will produce a concatenated string, if two operands for it are strings. If the same operation is implemented using the operator + differently, then this process is called as operator overloading. If the same operation is implemented using the methods differently, then this process is called as method overloading. It is important to note that Java does not support operator overloading on user-defined data types. Ex: 123+45=168 (two numbers) master+minds=masterminds (two strings) Agent+117=Agent117 (one number and one string) int add(int,int);

Java FAQs-Introduction to OOP

There are two types of polymorphism: static polymorphism (compile-time polymorphism or static binding or early binding), dynamic polymorphism (run-time polymorphism or dynamic binding or late binding). Static polymorphism refers to an entity that exists in different physical forms at the same time. This concept can be best explained by considering an example of a woman. A woman has to play the role of a wife, a mother, a sister, a daughter and an executive at the same time. In Java, static polymorphism is achieved with the help of method overloading. Dynamic polymorphism refers to an entity that changes its form depending on the circumstances. For example, a chameleon changes its color at the sight of an approaching enemy. In Java, this dynamic polymorphism is achieved with the help of method overriding.
Polymorphism

Static Polymorphism

Dynamic Polymorphism

Method overloading

Method Overriding

17.What is dynamic binding? Dynamic Binding is the process of linking a function call to code associated with it at run-time. Usually, the term binding refers to the linking of a procedure call to its associated code whenever it is made. Dynamic Binding (also known as late binding) means that the code associated with a given function call is not known until the time of call at run-time. It is associated with polymorphism and inheritance.
Shape Draw( )

Circle Draw( )

Box Draw( )

Triangle Draw( )

Consider the function Draw(). By inheritance, each object will have this function with its own implemented code. At run-time, the code matching the object under the current reference will be called. 18.What is message communication? Message communication is the process in which an object calls its function by sending arguments optionally to it.

Java FAQs-Introduction to OOP

An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language involves the following basic steps: 1. Creating classes that consists of both data and functions. 2. Creating objects from class definition and 3. Establishing communication among objects. Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. A message for an object is the request for execution of function and therefore will invoke a function in the receiving object that generates the result. Message passing involves specifying the name of the object, the name of the function and the information to be sent.
employee1.pay(amount);

Object

function

information

Each object has a life cycle. It can be created and destroyed. Communication with an object is feasible as long as it is alive. *************The End*************** Please give: (regarding this chapter) Your valuable comments Your precious suggestions Your priceless information about any forgot question to be added to this chapter to: dpradeep1982@gmail.com

Java FAQs-Introduction to OOP

You might also like