• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

override static method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What the answer for
can override static method in Java ???
=========================
public class AAA extends BBB
{
public static void main(String[] args)
{
AAA a;
BBB b;

b = new AAA();
b.p();
}

static public void p()
{
System.out.println("aaaaaaaa");
}
}
class BBB
{
static public void p()
{
System.out.println("bbbbbbbb");
}
}
=================
the output is bbbbbbbb.
I do not think it is override.
=================
What are your opinions? Thanks!
-ZZ

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're correct. I think calls to static methods get statically (no pun intended!) bound at compile time (per the variable type). This is because static members are not associated with an object instance.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also agree. Static methods cannot be overriden since they belong to the class and not an instance of the class.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though you cannot use the term 'override' per se, you can re-declare static methods in derived classes. The redeclared method hides the method declared in the superclass. However, ad DaB pointed out, since static methods are bound to Class types and not objects, you will not see the polymorphic behaviour displayed by overridden instance methods.
Ajith
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajith,
So if the options are like:
1.Static methods can not be overridden
2.Static methods can not be overridden to be non-static
I guess correct choice will be still 2 right?
It doesn't give you any error when you override it per say
but in effect doesn't really use most derived method.
What is the take on these two options from exam point?
Sarang
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ask me, both the answers are right. However, if you should choose only one, I will choose the second one.
Ajith
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic