Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link

Gautam Bhalla

Ranch Hand
+ Follow
since Mar 31, 2013
Gautam likes ...
Hibernate Eclipse IDE Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Gautam Bhalla

why doesn't this code is giving "here" as output? If I am using equals rather than == operator it is giving output "here"?
8 years ago
What is difference between getServletContext().setAttribute() and public data members in servlet classes?
8 years ago
why we use many-to-one to map one-to-one foreign key associations in hibernate?
Hye,got your answer thanks.then what was exact use of generics apart from collections.

8 years ago
java generics are resolved during compile time and also primitive operations on types must be resolved during compilation then why cannot we specify some operation on java generics?
I mean something like

8 years ago
As we all know that Strings objects are created in permanent generation area.which is never garbage collected.So,can String objects cause outofmemoryerror?
8 years ago

Jeanne Boyarsky wrote:Because interfaces automatically define anything to be public. For data, they are also static and final. For methods, just public.

Hi jeanne,

That was something I wanted to understand,why just public and why cannot protected?

As per context and as per technical feasibilities as well , interfaces are meant to be implemented by sub classes or extended by other interfaces;Any ways, If my members are protected I should still be able to access them in my sub-classes as intended.
8 years ago
why can't we have protected data members in an interface?
8 years ago

Ansh Sharma wrote:Hi Gautam ,

Many Thanks for the reply .
My doubt remains the same . Why can't we use the abstract class and have two abstract methods startFly() and calculateWeight()
instead of having Interface . For Example if tomorrow we find out some common behaviour between the classes (which in your case of Aeroplane and Mosquito might never happen ) we can have that concrete implementation in the abstract class .
Which design model of OOP is supported through this .. ?


Hi Ansh,

Aeroplane and mosquito were just a part of example but whenever we create a interface we never look for any specific thing rather we have a general idea that if something has to happen(flying in previous case) then its contract (i.e, associate functionality of startfly() and calculateweight()) should be taken as compulsion in order to make flying happen.

And as I already said In abstract class we might have a chance where startfly() functionality was already implemented.It means we have decided that all flying things should have startfly() functionality in a way specified in my abstract class which is wrong for modelling any kind of generalization.

Please do remember Abstract classes may talk about specialization but interface is just a implementation independent general contract whose actual realization depends upon more specialized sub classes.
8 years ago

Now I have 3 questions out of this code-::

1) why my java compiler is not permitting me to write implements before extends?

2) I have not given definition of disp() method of my interface but implemented the same method in class Cls as public method..It is giving compile time error like disp was not defined in ClsMain.Even though its definition has to be found as it is inherited from Cls by ClsMain?

3) Is it an example of multiple inheritance by classes in java?

8 years ago
Hi Ansh,

As you must already be knowing that interfaces does not contains method implementations it just contain method declarations and they are implicitly public abstract methods that are supposed to be overridden.
Technically there can be many answers to your question but logically from practical scenario,

Lets take an example of a flying object for which we want to calculate weight and implement its flying functionality after that.
FLY that contains two methods------- calculateWieght() and startFly()

Now,we know that an aeroplane can fly.we also know that a mosquito can also fly.
We also know that way we calculate weight of a mosquito is completely different from weight calculcation of aeroplane.

But both are adhering to common contract of flying and weight of both can also be calculated(irrespective of the way this is accomplished).

It means both of them are adhering to a common contract of capability of flying and weight calculation.(Although each of them does this in its own way).

So In JAVA interface let us realize this thing effectively as it imposes a kind of compulsion that if a object has to be able to fly then its weight must be calculated
and must implement startfly functionality.(As per example).

Now let us take the example of abstract classes(where we can have chance of having body of member function,say,startfly() in our case).Then ,Even though both will have their own calculateweight() functionality but In this case we are not getting compulsion but a kind of option that either aeroplane and mosquito should use a common way of flying (which is offcourse not possible) or they can implement this flying functionality of their own(by overriding the previous one).

So,technically we can realize the way interfaces work through abstract classes but this is something which is not effective to model a realistic scenario (or contract) as specified in above example.

So its not about unneccesary coding but it is all about effective modeling or real world objects via OOP(which is its prime feature).

The example may seem funny but I hope this notion would help you to have a better grip on the concept.

8 years ago
Thanks to all, I have already got what I expected from this forum..
8 years ago

Hye Henry,

From your post,It got the following flow->

compilation --------->then interpretation of byte code------>compilation of temporary native code + system profiling information---->Program execution.

Anyways If these are the steps then I got the concept clearly.
8 years ago

Jeff Verdegan wrote:A language doesn't have an inherent property of being compiled or interpreted. Any language could be executed either way. (Or at least most languages. There may be some subtle detail that prevents it being universal).

In normal usage, Java source code is definitely compiled to Java bytecode--the machine code for the JVM. But then at runtime, that compiled bytecode is interpreted, at least at first, by the JVM. Some of the bytecode will be compiled to native code on the fly.

In the end, trying to classify Java as a "compiled langauge" or "interpreted language" is an oversimplification, and pointless.

Hi Jeff,

Anyways a language has to stand in either category or both.

To me role of JIT is not clear.Please help me to understand its concept during java program interpretation.On one hand we are interpreting our java byte code via JVM interpreter on the other hand we are saying that it is JIT Compiled that is an actual compiler.

And as you said
1)java is first compiled to bytecode.
2)then at runtime byte code is interpreted or some byte code can be compiled to native code on the fly.

Does it mean that JVM needs to resolve whether it need to interpret or compile it via JIT during runtime??
8 years ago

Earlier I used to think that java is compiled as well as interpreted but I got confused when I got across an article in wikepedia which states

The JIT compiler reads the bytecodes in many sections (or in full, rarely) and compiles them dynamically into machine language so the program can run faster. Java performs runtime checks on various sections of the code and this is the reason the entire code is not compiled at once.[2] This can be done per-file, per-function or even on any arbitrary code fragment; the code can be compiled when it is about to be executed (hence the name "just-in-time"), and then cached and reused later without needing to be recompiled.Just-in-time_compilation

.(see overview section)

I know this has been discussed a lot of times but still I request the forum members to help me clarify my concepts.
8 years ago