?
Solved

Confusing

Posted on 2005-03-17
4
Medium Priority
?
192 Views
Last Modified: 2010-03-31
I have class test defined as below
class Test
            {
                  int x;
                  private static int count=0;

                  public Test()
                  {
                        ++count;
                        System.out.println ("Count: " + count);

                  }
                  public static void show() {
                  System.out.println ("Show method in Test class");
                  }

                  public static int getCount() {
                        return count;
                  }

            }

Another class Quest1 defined as below

    class Ques1 extends Test
   {
        static int ct;
      public static void show()
      {
          System.out.println("Show method in Q2 class");
     }
      public static void main(String[] args)
      {

              int x=012;
                  if ( x==10.0) { System.out.println("test" + x); }
                 Test t = new Test();
                 t.show();
                 
            System.out.println("about to instantiate Ques1");
                Ques1 q = new Ques1();
                 q.show();
                 t=q;
                 System.out.println("t:" + t);
                 System.out.println("t:" + q);
                 t.show();
                 q.show();


                 
      }
  }

I have made reference of t equal to reference of qin quest1.java
when i printed their referenced it is giving me same.
but when i called t.show() it is calling the test class method show() . I am confused. I am thinking since i made t=q i thought it will call show method of quest class instead of test class ??

Please explain me
0
Comment
Question by:Nandi11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 13570161
thats because the method is static. remove the static from the definition of show and you will get the expected result.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13570612
objects is right, but just to make it clearer.
When you are calling a static method on an object the call will be done on the class of type and not the type of the instance (this is set during the compilation process).
So, because t is declared as type Test then Test.show will be called.
To check that remove the show method from Test and you will see that you get a compilation error regardless the fact that t object is an instance of a subclass of Test (Ques1)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question