my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Need some clarification for OOPS concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need some clarification for OOPS concepts" Watch "Need some clarification for OOPS concepts" New topic
Author

Need some clarification for OOPS concepts

G Nair
Greenhorn

Joined: Sep 28, 2009
Posts: 17
I have some of the doubts for OOPS concepts of Java , need more clarification about what happens in the background

1. Why cant we assign a super class object to sub class refrence ?
2. why its not necessary to have a same return type for overloaded methods and overriden methods ?
3. when (compile/run time) is it getting decided that which overladed / overriden method has to be called and why ?
4. exact diffrent between compiler and interpreter and how do they work in the background ?
5. what is Java compiler ? compiler + interpreter ? or only interpreter ? need more clarification on this ?


Please provide me the link or clarification on this above ... and also it will be great if i get alink for such kind of basic details ...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

G Nair wrote:1. Why cant we assign a super class object to sub class refrence ?

Take the following example:
- super class Animal
- sub classes Dog and Cat
You can use a cast to force the compiler to still accept it, but then you'll get a ClassCastException.

2. why its not necessary to have a same return type for overloaded methods and overriden methods ?

Overloaded methods are completely different methods that have only one thing in common - the name. Other than that there is (syntactically) nothing in common.
Overridden methods must have the same return type, or a narrower. This is called covariant return. Look it up.

3. when (compile/run time) is it getting decided that which overladed / overriden method has to be called and why ?

For overloaded methods the compiler decides which one to use. During runtime the JVM uses the actual instance to find out from which class the method should be called. This must be done during runtime because during compile time the type simply isn't known.

4. exact diffrent between compiler and interpreter and how do they work in the background ?

I'm sure you can find an answer if you SearchFirst. Google and Wikipedia should be more than able to help you with this.

5. what is Java compiler ? compiler + interpreter ? or only interpreter ? need more clarification on this ?

The name says it all. The Java compiler is only a compiler. The JVM is an interpreter that also includes a JIT compiler. Again, Google or use our search for more info.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
G Nair
Greenhorn

Joined: Sep 28, 2009
Posts: 17
Thank you so much Rob , These kind of questions have been asked for interviews wherever i go , so would like to know more about the which book from the following can help me

1.Complete Reference Java
2.Head First Java
3. SCJP book by cathy

Please let me know if you have any like for the same .
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

G Nair wrote:which book from the following can help me
1.Complete Reference Java
2.Head First Java
3. SCJP book by cathy

apart from this, there is one more *Thinking in Java* . if you want to know about interpreter more then look into the JVM specifications
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need some clarification for OOPS concepts