• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Base base=new Sub()??

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,all:
I am very comfused about this: Base base=new Sub() and the Sub extends Base, the base points a Sub object (new Sub()).
Why can I just use the Sub's instance method? and the others method (static method) and member variables are not.
As I know , the base indicate a Sub object.
Please help me! Thanks a lot.
rickluo
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a clear explanation in JLS (chapter 6 I think)
anyway you can follow the extract below too..
<-- got this somewhere from the net -->
When Is an �Overridden� Method Not Really Overridden?
OK, so I admit it. The question posed in the title of this item is really a trick question. The goal of this item, though, is not to trick you, but rather to help you understand the concept of overriding methods. I�m sure that you have picked up a book or two that began explaining Java by pointing out the three main concepts of object-oriented programming: encapsulation, inheritance, and polymorphism. Understanding these three concepts is crucial to understanding the Java language. Understanding method overriding is crucial because it is a key part of inheritance.
Overriding an instance method is covered in Item 5. This item covers overriding static methods. If you didn�t know there was a difference, then this item (as well as Item 5) is for you. If you have started breaking out into a sweat and find yourself screaming �You can�t override a static method!,� then you may want to relax a bit and move on to the next item. But first, see if you can figure out what the output of the following example will be.
This example is taken from the Java Language Specification, section> 8.4.8.5:

class Super
{
static String greeting()
{
return �Goodnight�;
}
String name()
{
return �Richard�;
}
}
class Sub extends Super
{
static String greeting()
{
return �Hello�;
}
String name()
{
return �Dick�;
}
}
class Test
{
public static void main(String[] args)
{
Super s = new Sub();
System.out.println(s.greeting() + �, � + s.name());
}
}
The output from running class Test is:

Goodnight, Dick

If you came up with the same output, then you probably have a good understanding of method overriding. If you didn�t, let�s figure out why. We will start by examining each class. Class Super consists of the methods greeting and name. Class Sub extends class Super and also has the greeting and name methods. Class Test simply has a main method.
In line 5 of class Test, we create an instance of class Sub. Make sure that you understand that even though variable s has a data type of class Super it is still an instance of class Sub. If that�s a bit confusing, then you can think of it this way: Variable s is an instance of class Sub that is cast to type Super. The next line (6) displays the value of the returned by s.greeting(), followed by the string �,� and the value returned by s.name(). The key question is �Are we calling the methods of class Super, or are we calling the methods of class Sub?� Let�s first figure out if we are calling class Super�s name method or class Sub�s name method. The name method in both the Super class and the Sub class is an instance method, not a static method. Because class Sub extends class Super and has a name method with the same signature as its parent class, the name method in class Sub overrides the name method in class Super. Because variable s is an instance of class Sub and class Sub�s name method overrides class Super�s name method, the value of s.name() is �Dick.�
We are half way there. Now we need to figure out if the greeting method is being called by class Super or by class Sub. Notice that the greeting method in both the Super class and the Sub class is a static method, also known as a �class� method. Despite the fact that the greeting method of class Sub has the same return type, the same method name, and the same method parameters, it does not override the greeting method in class Super. Because variable s is cast as type Super and the greeting method of class Sub does not override the greeting method of class Super, the value of s.greeting() is �Goodnight.� Still confused? Follow this rule: �Instance methods are overridden, static methods are hidden.� If you were the reader who was screaming �You can�t override a static method!,� you are absolutely right
Now you may be asking this: �What is the difference between hiding and overriding?� You may have not recognized it, but we actually just covered the difference in the Super/Sub class example. Using a qualified name can access hidden methods. Even though variable s is an instance of class Sub and the greeting method of class Sub hides the greeting method of class Super, we can still access the hidden greeting method by casting variable s to class Super. Overridden methods differ because they cannot be accessed outside of the class that overrides them. That is why variable s calls class Sub�s name method and not class Super�s name method.
This item is a short explanation of a sometimes confusing aspect of the Java language. Perhaps the best way for you to understand the difference between hiding a static method a overriding an instance method is for you to create a few classes similar to class Sub and class Super. Remember, instance methods are overridden and static methods are hidden. Overridden methods cannot be accessed outside of the class that overrides them. Hidden methods can be accessed by providing the fully qualified name of the hidden method.
Now that we understand that the answer to the question posed by the title of this item is �never,� I have a few more tidbits for you to keep in mind:
� It is illegal for a subclass to hide an instance method of its super class with its own static method of the same signature. This will result in a compiler error.
� It is illegal for a subclass to override a static method of its super class with its own instance method of the same signature. This will also result in a compiler error.
� Static methods and final methods cannot be overridden.
� Instance methods can be overridden.
� Abstract methods must be overridden.
<-- notes end here -->

[This message has been edited by Subramaniam Venkatesan (edited March 01, 2001).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your name "rickluo" does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Thanks!
Ajith
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subramaniam, Thank You .....for the good explaination. Now I got it....
 
Wasim Ahmed
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subramaniam, I have performed some sugery on your code below could you explain this to other's. It might be helpful.
 
Subramaniam Venkatesan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wassim, those are not my notes. I downloaded from the web sometime back.ok.
1 class Super1
2 {
3 static int greeting(int c)
4 {
5 return c;
6 }
7 String name()
8 {
9 return "Richard";
10 }
11 }
12 class Sub extends Super1
13 {
14 static String greeting(String b)
15 {
16 return "Hello";
17 }
18 String name()
19 {
20 return "Dick";
21 }
22 }
23 class Test
24 {
25 public static void main(String[] args)
26 {
27 Super1 s = new Sub();
28 System.out.println(Sub.greeting("I am new") + ", " + ((Sub)s).name());
29 }
30 }
Wasim, let me see what you have done. Remember, static methods are hidden and instance methods are overridden. but when you put a different argument in the any method and if there are atleast 2 methods it is overloading. Overloading methods
can have different return types but their arguments differ.
1.)Now Sub.greeting(String) is straight forward will call the static method of Sub.
2.)For, s.greeting("I am new"); -->it is a compile error. because s is of variable type Super and Super has no proper signature of the above method, therefore it is compile time err,
eventhough there exist a method in Sub.
3.) What about Sub.greeting(33); --> valid and is executed at run time.
4) If method in Super is like this...private static int greeting(int c) then
calling Sub.greeting(44) or s.greeting(44) is invalid at compile time.
5.) If the above is true and then you place the main method of Test class inside Super then,
s.greeting(44) is valid and Sub.greeting(44) gives compiler err, because greeting(int) is private.
Now, consider this,
Super1 s=new Sub();
Sub sub1=s;

s is a type of Super class and it is being assigned to
a subclass variable. this is down-casting and the compiler will complain.
Sub sub1=(Sub) s; is valid at compile time because it is an explicit cast and Sub is the childclass of Super. what about at runtime. s is pointing to an object of Sub and we are assigning such reference to a base class variable. this is perfect. if s had been the reference to base class then it can only be stored in a type of Super or its parent class (Object class).
1.)Hence, (Sub)s.name() will call the (base-class) Sub's name() method.
2.)If there is no method like name() in Sub, then name() method is inherited from Super and that will be called.
What would you expect if the static int greeting(int) had been declared private. Try it out
and see. Remember private methods are visible only within its outermost parent class or the
top level class.

hope this clears. try lots of combinations changing the greeting method(int) to greeting(String) in Super and try all combinations with the above methods making it private. then move your static main method from Test to other class and try out the combinations.
<cautious> I may have typed few things wrongly.. so takenote of it.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subramaniam and all:
Thank you very much! I think I have understanded the hiding and
Overriding.
By the way, I have changed my username from rickluo to rick joy.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic