File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Object oriented concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Object oriented concept" Watch "Doubt in Object oriented concept" New topic

Doubt in Object oriented concept

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I know the basics of Object orientation and all,or atleast i assume so.But i came across a question in one my interviews,where in the interviewer asked me to describe object orientation and its advantages.I told about abstraction,encapsulation and told the interviewer that it helps in code re-usability ,extensibility etc.

The interviewer asked that how does object orientation helps in code re-usability. I told him about "inheritance" ,and "polymorphism". He was like why do you need "inheritance",why not have some class/classes which has function/functions and have the "reusable" code in it. I told him that if we do like that ,we are deviating away from the concepts of "objects". Can anyone tell me how better or the better answer for this....

Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Jagadeeswara Yaramala
Ranch Hand

Joined: Jul 28, 2010
Posts: 46
Some reasons why inheritance is useful (what I think are):
1) Code written in super class is tested once only and sub-classes can use the tested code/behavior. So, testing time gets reduced considerably.
2) Real world actually has inheritance relationships (For example, in insurance domain, a Contract can be a super class and Savings, Protection, Car, Health insurance contracts are sub-classes of the super class 'Contract').
3) Code maintenance gets easier, because of the number of places where code needs to be changed (and tested) is reduced.
4) Through interface inheritance, a class can be made to be useful in multiple scenarios (a kind of polymorphism).
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Jagadeeswara!
But again what you are telling me is little theoretical..i understand what you are trying to say but just try to compare with the point i mentioned in my post..i.e

He was like why do you need "inheritance",why not have some class/classes which has function/functions and have the "reusable" code in it. I told him that if we do like that ,we are deviating away from the concepts of "objects". Can anyone tell me how better or the better answer for this....

All 4 points which you said are valid,but then you can accomplish the same by say having a class with all the common code put in it...I don't need any inheritance to do that,then why inheritance.
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

I assume you are saying, why not have some utility classes rather than inheritance in place,so that when ever you need to use the same common code, you can invoke the utility method.

If we use inheritance we can take advantage of polymorphism, if we have common code in utility classes, we can not take advantage of polymorphism and how about inheriting the state, how do we do that if we don't have inheritance.

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
I think you should search the web for Java interview questions, learn, and prepare to answer some such or unknown questions arise on an interview.

Below is a description of OOP that may help you answer the question impressively.

Hope it help...

Object Oriented Programming or OOP is the technique to create programs based on the real world.
OOPs and Its Concepts in Java

Brief Introduction to OOP
Object Oriented Programming or OOP is the technique to create programs based on the real world. Unlike procedural programming, here in the OOP programming model programs are organized around objects and data rather than actions and logic. Objects represent some concepts or things and like any other objects in the real Objects in programming language have certain behavior, properties, type, and identity. In OOP based language the principal aim is to find out the objects to manipulate and their relation between each other. OOP offers greater flexibility and compatibility and is popular in developing larger application. Another important work in OOP is to classify objects into different types according to their properties and behavior. So OOP based software application development includes the analysis of the problem, preparing a solution, coding and finally its maintenance.

Java is a object oriented programming and to understand the functionality of OOP in Java, we first need to understand several fundamentals related to objects. These include class, method, inheritance, encapsulation, abstraction, polymorphism etc.

Class - It is the central point of OOP and that contains data and codes with behavior. In Java everything happens within class and it describes a set of objects with common behavior. The class definition describes all the properties, behavior, and identity of objects present within that class. As far as types of classes are concerned, there are predefined classes in languages like C++ and Pascal. But in Java one can define his/her own types with data and code.

Object - Objects are the basic unit of object orientation with behavior, identity. As we mentioned above, these are part of a class but are not the same. An object is expressed by the variable and methods within the objects. Again these variables and methods are distinguished from each other as instant variables, instant methods and class variable and class methods.

Methods - We know that a class can define both attributes and behaviors. Again attributes are defined by variables and behaviors are represented by methods. In other words, methods define the abilities of an object.

Inheritance - This is the mechanism of organizing and structuring software program. Though objects are distinguished from each other by some additional features but there are objects that share certain things common. In object oriented programming classes can inherit some common behavior and state from others. Inheritance in OOP allows to define a general class and later to organize some other classes simply adding some details with the old class definition. This saves work as the special class inherits all the properties of the old general class and as a programmer you only require the new features. This helps in a better data analysis, accurate coding and reduces development time.

Abstraction - The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface).

Encapsulation - This is an important programming concept that assists in separating an object's state from its behavior. This helps in hiding an object's data describing its state from any further modification by external component. In Java there are four different terms used for hiding data constructs and these are public, private, protected and package. As we know an object can associated with data with predefined classes and in any application an object can know about the data it needs to know about. So any unnecessary data are not required by an object can be hidden by this process. It can also be termed as information hiding that prohibits outsiders in seeing the inside of an object in which abstraction is implemented.

Polymorphism - It describes the ability of the object in belonging to different types with specific behavior of each type. So by using this, one object can be treated like another and in this way it can create and define multiple level of interface. Here the programmers need not have to know the exact type of object in advance and this is being implemented at runtime.


"Your life is in your hands, to make of it what you choose."
I agree. Here's the link:
subject: Doubt in Object oriented concept
It's not a secret anymore!