• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

explain the actions taken at the compile time and at the runtime.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Shape
{
void erase(){ }
void draw(){ }
}

class Triangle extends Shape
{
void erase(){ }
void draw(){ }
}


class Line extends Shape
{
void erase(){ }
void draw(){ }
}

class Circle extends Shape
{
void erase(){ }
void draw(){ }
}

public class OverridingTest
{
public static void main(String arr[])
{
Circle c = new Circle();
Triangle t= new Triangle();
Line l= new Line();
}

void doStuff(Shape s)
{
s.erase();
s.draw();
}
}
 
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there. Please ShowSomeEffort. I don't think anyone here understands what you want to ask, so please take time to explain clearly what you want to know. When you post code, please UseCodeTags.
 
gauravkv gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here i want to know how the compiler will perform syntax checking for doStuff method at compile time when selection of method will be done at runtime.
since till the runtime we dont know, which doStuff will be called, so how compiler will perform check for syntax for the same at compiler.

so i need to know the detailed description of all the action performed at compile time for the given code and runtime actions too to clear this doubt.

Thanks for asking the description of question for more clarity...

 
Stephan van Hulst
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if the compiler can't know exactly what method is being called at runtime, it knows that every Shape must have erase() and draw() methods. It says so right in the Shape class, right?

The compiler doesn't care which method gets called eventually, as long as the signature is correct. Does this clear matters up for you?
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Jeff Verdegan .... is it ok to reply here???
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:hey Jeff Verdegan .... is it ok to reply here???



Of course it's okay. All that is asked is that you follow the site's guidelines. Links to the main ones are listed in the "How to Answer Questions" FAQ: https://coderanch.com/how-to/java/HowToAnswerQuestionsOnJavaRanch

Thanks for your participation, and if you have any questions about site etiquette, etc., you can start a topic in the Ranch Office forum: https://coderanch.com/forums/f-10/Ranch-Office.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Nikhil Sagar wrote:hey Jeff Verdegan .... is it ok to reply here???



Of course it's okay. All that is asked is that you follow the site's guidelines. Links to the main ones are listed in the "How to Answer Questions" FAQ: https://coderanch.com/how-to/java/HowToAnswerQuestionsOnJavaRanch

Thanks for your participation, and if you have any questions about site etiquette, etc., you can start a topic in the Ranch Office forum: https://coderanch.com/forums/f-10/Ranch-Office.



Okay thanks Sir Jeff.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, Compiler checks all the grammar here.Then type of variable (like in s.erase() compiler checks type of variable s and that is Shape type)and checks the existence of the method into the class of the reference variable(means checks the existence of erase() method in Shape class ) if it is there then attach the method to it (means s.erase() is a valid statement) if not then throws a error.At run time object comes into existence and the reference id of the objects assigned to the reference variables then jvm checks the calling of a method (like s.erase()) on the basis of the object behind the reference variable (means if it was in your code like

then at compile time compiler checks the erase method in the Shape class but at run time the method inside the Triangle class would call because jvm checks the object behind reference variable not the type of the reference variable.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i said, "i think". There are so many experienced people around here so please forgive me if there is mistake in my knowledge.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's fundamentally correct, if a bit imprecise in its terminology.
 
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find the BCEL website useful. Start by looking at their manual. It might be rather old, but I don’t think there will be any big changes.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic