473,699 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java questions

1. Can subclass inherit the static methods?

2. Can you overide the static menthods?
Jul 17 '05 #1
2 3446
-----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/KmiqwxczzJRavJY RAl8qAJ9wZeNP2T giz/Vt23eODIrycwiLg ACdFv0O
LSlj90XqpaIohDj KtkOj+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*******@hotm ail.com> wrote in message
news:rW******** ************@ne ws0.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/KmiqwxczzJRavJY RAl8qAJ9wZeNP2T giz/Vt23eODIrycwiLg ACdFv0O
LSlj90XqpaIohDj KtkOj+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
45990
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 know PHP, BASIC, and of course HTML. I'll be 15 years old in September. I am interested in programming GUI applications. I am also interested in programming games. I know that I should learn C++ to program games, but would learning Java make the...
8
1700
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 have to say that Java SWING sucks BIG TIME for developing and deploying desktop applications (especially on Windoze). Unfortunately, I have been dragged kicking and screaming back to my old haunts - but the landscape has changed somewhat and I...
1
9635
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
20
2546
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
6166
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 the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip 2000 Interview questions of .NET , JAVA and SQL Server Interview questions (worth downloading it)
2
6956
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
2
7219
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 yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
0
1097
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 randomly(random questions).in my frame i have next button now if i click next button, next question is coming from the table. i want to get random questions from the table.for example if the test has 10 questions then 10 random questions have to be...
1
2056
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 notepad. But i need to call it from java. My problem is that i need to read each question as a whole sentence. The way it is written it is broken, writing a word on each line. How can i read it as a whole sentence? This is the source code i have...
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9197
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6549
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.