File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overriding. Please help

 
vadiraj vd
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Balamurugan Kandasamy
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shishir Choudhary
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javachamp.com/public/bookshelf.xhtml


download free
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic