Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Java Chapter 4

Q. What is Inheritance?

Ans :- Classes inherit state and behavior from their superclass. Inheritance provides a very helpful concept of code reusability.

Generally speaking, objects are defined in terms of classes. You know a lot about an object by knowing its class. If I told you it was a bicycle, you would know that it had two wheels, handle bars and pedals.

Object-oriented systems take this a step further and allow classes to be defined in terms of other classes. For example, mountain bikes, racing bikes and tandems are all different kinds of bicycles. In object-oriented terminology, mountain bikes, racing bikes and tandems are all subclasses of the bicycle class. Similarly, the bicycle class is the superclass of mountain bikes, racing bikes and tandems.
Each subclass inherits state (in the form of variable declarations) from the superclass. Mountain bikes, racing bikes and tandems share some states: cadence, speed and the like. Also, each subclass inherits methods from the superclass. Mountain bikes, racing bikes and tandems share some behaviors: braking and changing pedaling speed. However, subclasses are not limited to the state and behaviors provided to them by their superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. Tandem bicycles have two seats and two sets of handle bars; some mountain bikes have an extra set of gears with a lower gear ratio. Subclasses can also override inherited methods and provide specialized implementations for those methods. For example, if you had a mountain bike with an extra set of gears, you would override the "change gears" method so that the rider could actually use those new gears.

You are limited to just one layer of inheritance. The inheritance tree, or class hierarchy, can be as deep as needed. Methods and variables are inherited down through the levels. The further down in the hierarchy a class appears, the more specialized is its behavior.

The Benefits of Inheritance

·         Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times.
·         Programmers can implement superclasses that define "generic" behaviors (called abstract classes). The essence of the superclass is defined and may be partially implemented but much of the class is left undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

Q. Explain the Term Abstraction.

Ans:- In java, classes are used to categorize data in order to model real life systems. Abstraction is this process of categorizing data.

Consider that a person is looking for a frame in an optician’s shop. To be able to choose a frame from amongst the various types of frames available, he has to first identify the attributes he is looking for (say, he looks for a frame of golden color with photo-chromatic lens). Once he has identified the attributes, he has with him a category or class of frames of golden color with photo-chromatic lens from which he can readily pick any one frame. Similarly, to model any real life object in OOPS, an “object” has to be instantiated from a specific “class”. This basic process of forming a class is known as abstraction. In the above example, frames of golden color with photo-chromatic lens become a specific class of frames and one such frame represents an object of that particular class.

Q. Define the Polymorphism and benefits of OOP's.

Ans:- Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate, actions.
Each shape class has its own version of the draw function that provides the appropriate action for an object of that class. A rectangle object’s draw function displays a rectangle, an Ellipse draw function displays an ellipse, and so on.
Polymorphism is another important OOP concept. Polymorphism means the ability to take more than one form. For example, an operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. For example, consider the operation of addition. For two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation. Fig 1.9 illustrates that a single function name can be used to handle different number types of arguments. This is something similar to a particular word having several different meanings depending on the context.
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 operation may be accessed in the same manner even though specific actions associated with each operation may differ. Polymorphism is extensively used in implementing inheritance.

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 run-time.
·         It is associated with polymorphism and inheritance.

Consider the procedure “draw” in fig 1.8. 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 called.


In general terms overloading means more than one job done by a single entity. For example a man at a time can be a Father, can be a brother, can be a son and so on. The single person bearing all the relationship. In Object Oriented Programming if a method depending on some information does different job at different times that method is overloaded .Overloading is nothing but a kind of polymorphism.


Inheritance is a mechanism of inheriting the traits of parents but some times some properties should be modified according to the need like a son inherits legs from his parents but his walking style is different. This is Overriding. Using this concept derived class can modify the features which it inherits from it’s base class. It is again a kind of polymorphism and very important in terms of dynamic decisions.

Benefits of OOP

Through inheritance, we can eliminate redundant code and extend the use of existing classes.
We can build programs from the standard working modules, which 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 co-exist without any interference.

It is possible to map objects in the problem domain to corresponding objects in the program.

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.

Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler.

Software complexity can be easily managed.

No comments:

Post a Comment