Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt in static- class's -instance .method()

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Some{
static class A{void process()throws Exception
{}
}
static class B extends A{
void process(){}
}

public static void main(String args[])
{ A x=new B();
x.process(); //this calls A's method

}}}
In case of regualr classes ,
x.process() call depends on object type ,selection of method depends on reference type.

But why in case of static classes ,method call also depends on refernce type.

Please help me explaining this concept



Here ,in case of regualr classes ,
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the difference between static and instance. When the method is static, it is irrelevant what object the references points to. It could be null, and it wouldn't matter. What matters is the type of the reference.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By running the above code x.process() invokes B's method, so here polymorphism apply on selecting which method at runtime.

To get the code to compile you ll need to wrap the call of x.process() with try/catch because at compile time A's method that throws checked exception is one that is matched.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahmed,
My program is running without wrapping and calling the A's process().
which is only because A being static inner class.
Otherwise ,we know polymorphism works and (Runtime types or object types )B 's process() is called.
This is my Question,why Reference type 's method is called
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lucky J Verma:

My program is running without wrapping and calling the A's process().



1. how is it possible to call a method that throws a checked exception without handle or declare!
2. x.process() calls B's method and you can prove it



output: B
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ahmed.
I got the concept now.Only static thing was confsing me.
My apologies...i messed it up.You were right.
Thank you so much
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is

allowed, when the syntax for instantiating a static nested class should be



Or is it the first way is just as good !
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are instantiating the nested class from the enclosing class you can use simple names such as A x = new B();
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic