473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Java questions

1. Can subclass inherit the static methods?

2. Can you overide the static menthods?
Jul 17 '05 #1
2 3440
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

NepaliGuy wrote:
1. Can subclass inherit the static methods?

2. Can you overide the static menthods?


Hi,
Both of these are impossible. Ignoring reflection, the only way to
call a static method is if you already know the classname at compile
time. If you already know the classname, it makes no sense to inherit
a static method, since it would never get called anyway. I'm aware
that you can call static methods through an instance of a class, but
this is considered bad. Having just run a quick test, I realize why:
the static method called is selected by the declared type of the
variable, not the actual type of the object! For example, given
classes A and B, where B extends A, and where both define a static
method x(), the following code:

A a = new B();
a.x();

actually ends up calling A.x(), since the runtime type of a is
ignored.

The fact is, there is no practical use for inheriting or overriding
static methods, since there is no way for a subclass's version to be
called polymorphically anyway. You can define a new static method of
the same name in a class that just happens to be a subclass, though I
assume this doesn't count as overriding.

If you do happen to be using reflection, and need a class (whose name
is to be determined at runtime) to have a certain static method, your
only option is to write this requirement in documentation and hope
the client obeys it.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/KmiqwxczzJRavJYRAl8qAJ9wZeNP2Tgiz/Vt23eODIrycwiLgACdFv0O
LSlj90XqpaIohDjKtkOj+A4=
=ofLX
-----END PGP SIGNATURE-----
Jul 17 '05 #2
Hey thanks Chris. It was great explaination. I like the way you explained
the philosphy behind the reason.

"Chris" <ch*******@hotmail.com> wrote in message
news:rW********************@news0.telusplanet.net. ..
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

NepaliGuy wrote:
1. Can subclass inherit the static methods?

2. Can you overide the static menthods?


Hi,
Both of these are impossible. Ignoring reflection, the only way to
call a static method is if you already know the classname at compile
time. If you already know the classname, it makes no sense to inherit
a static method, since it would never get called anyway. I'm aware
that you can call static methods through an instance of a class, but
this is considered bad. Having just run a quick test, I realize why:
the static method called is selected by the declared type of the
variable, not the actual type of the object! For example, given
classes A and B, where B extends A, and where both define a static
method x(), the following code:

A a = new B();
a.x();

actually ends up calling A.x(), since the runtime type of a is
ignored.

The fact is, there is no practical use for inheriting or overriding
static methods, since there is no way for a subclass's version to be
called polymorphically anyway. You can define a new static method of
the same name in a class that just happens to be a subclass, though I
assume this doesn't count as overriding.

If you do happen to be using reflection, and need a class (whose name
is to be determined at runtime) to have a certain static method, your
only option is to write this requirement in documentation and hope
the client obeys it.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/KmiqwxczzJRavJYRAl8qAJ9wZeNP2Tgiz/Vt23eODIrycwiLgACdFv0O
LSlj90XqpaIohDjKtkOj+A4=
=ofLX
-----END PGP SIGNATURE-----

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
8
by: Beatrice Rutger | last post by:
Hi, I am a previous Micro$oft desertee (moved from VB/VC++ to Java before this whole DOTNET thing) because I had several issues with Micro$oft. I am not completely in love with Windoze, but I...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
20
by: mayershome | last post by:
Hi! I'dont have any experiences in programming... what language should I start learning???? C? C++ or Java`? greetz
0
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
0
by: neelesh kumar | last post by:
sir, i have a java applet.i am using msaccess database . Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"). in my program i want to get questions from access table questions in a new frame...
1
by: saytri | last post by:
Hi i'm a student studying java (i'm still a beginner). i have a project were i have to do a quiz with a set of questions. So i think that i have to type the questions in a textfile for example in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.