aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overriding. Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overriding. Please help" Watch "Method Overriding. Please help" New topic
Author

Method Overriding. Please help

vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Hi All,
Below is the explaination from Mughal about Method Overriding.

"When a method is invoked on an object using a reference, it is the 'class of the current object'
denoted by the reference, not the type of the reference, that determines which method
implementation will be executed."
-page no. 181.
This is true in case of non-static methods. But how about static methods?
Well my doubt is in the code below the method from superclass is called
when the superclass reference is pointing to a subclass object.

CODE
---------------

<PRE>

class Super
{
public static void print()
{
System.out.println("Hello, this is from super class.");
}
}
public class OverridingStaticMethodDemo extends Super
{
public static void main(String a[])
{
Super sup; // Obj ref of superclass.
OverridingStaticMethodDemo sub = new OverridingStaticMethodDemo();
// obj of subclass.
sup = sub; // asigning subclass obj to superclass ref.
sup.print(); // line 1.
// Oops, the meth() of superclass is called.
sub.print(); // line 2.
// Oops, the meth() of subclass is called.
}

public static void print() //Try to Override static method.
{
System.out.println("Hello, this is from sub class.");
}
}

</PRE>
----------------------

In this code at line 1 print() method of superclass is called. How? according to
Mughal this should call print() of subclass. Is that static method can't be overriden
is that the reason?
Regards,
----------
vadiraj
------------------
*************************
There's a lot of I in J.
*************************
[This message has been edited by vadiraj vd (edited December 20, 2000).]


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
Hi Vadiraj,
If you could revisit the starting of the same paragraph in the page you have given, you can find that the author has specified for invoking instance method on an object which means it is not for static.
MKBala...
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Hi Balamurugan,
Thanks for your reply. I did silly mistake, Sorry.
Regards
--------
vadiraj

------------------
*************************
There's a lot of I in J.
*************************
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

hi friend,
when you are declaring the reference variable of type superclass and assign it to a sub class object(Polymorphism),you can access only the instance metod of subclass and the other things(instance variable,static variable and static method) of superclass will be called.


Why do we fall? So that we can learn to pick ourselves up.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Static methods cannot be overriden. they however can be hidden by a method with same name and signature(as the superclass method). remember overridding is only for instance methods. there is NO overridding for instance variables, static members. consider following expression

Super sup = new OverridingStaticMethodDemo();
sup.print();

compiler will only look at TYPE of the reference variable which is of the type Super and will invoke Super's print(). there will NOT be overridding(dynamic dispatch/switch). even if you had done
Super sup = null;
sup.print(); // there WON'T be NPE. infact print method of super will be called. So the only thing compiler see is type of reference variable.


also keep in mind that while hiding or overridding the access specifiers cannot be more restrictive than the superclass method. this is obvious in overridding but it also applies when a method gets hidden by method with same signature in subclass.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

Hi guys. Thanks for being helpful, but the original question was asked in 2000, so I doubt they're still looking for an answer.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

oh my gosh. thanks matthew. i didn't notice date of posting of OP. was misled by the recent post of Mr Saumyaraj zala.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Matthew Brown wrote:Hi guys. Thanks for being helpful, but the original question was asked in 2000, so I doubt they're still looking for an answer.

Gosh!! As soon as a question is fired, the "impeccable" mind starts acting...


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Shishir Choudhary
Greenhorn

Joined: Sep 21, 2011
Posts: 4
http://www.javachamp.com/public/bookshelf.xhtml


download free
Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 57
Can anyone help me to provide the solution for the below question?


You are given a class hierarchy with an instance of the class Dog. The class Dog is a child of mammal and the class Mammal is a child of the class Vertebrate. The class Vertebrate has a method called move which prints out the string "move". The class mammal overrides this method and prints out the string "walks". The class Dog overrides this method and prints out the string "walks on paws". Given an instance of the class Dog,. how can you access the ancestor method move in Vertebrate so it prints out the string "move";
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Ilakya Mukunth wrote:Can anyone help me to provide the solution for the below question?


You are given a class hierarchy with an instance of the class Dog. The class Dog is a child of mammal and the class Mammal is a child of the class Vertebrate. The class Vertebrate has a method called move which prints out the string "move". The class mammal overrides this method and prints out the string "walks". The class Dog overrides this method and prints out the string "walks on paws". Given an instance of the class Dog,. how can you access the ancestor method move in Vertebrate so it prints out the string "move";


This topic is 12 years old.You should start a fresh topic.


OCPJP 6 86%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overriding. Please help