473,405 Members | 2,310 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,405 software developers and data experts.

static functions

Hi,

why am I not allowed to override a static function of the base class?

class B{
static out () {}
}
class D extends B {
out() {}
}
Oct 25 '06 #1
3 8242
r035198x
13,262 8TB
Hi,

why am I not allowed to override a static function of the base class?

class B{
static out () {}
}
class D extends B {
out() {}
}
First let's make the code more legal java code by providing a return type to the method.

Expand|Select|Wrap|Line Numbers
  1. class B{
  2. public static void out () {}
  3. }
  4. class D extends B {
  5. public void out() {}
  6. }
This would not compile because static means that the method is not part of the class. You cannot therefore say that because B has out(), D should have out since it extends B. The reason you cannot say this is because B does not have out(out is static in B). Besides out in B is static but in D out is not static. Which brings us to what is allowed
Expand|Select|Wrap|Line Numbers
  1. class B{
  2. public static void out () {}
  3. }
  4. class D extends B {
  5. public static void out() {}
  6. }
Oct 25 '06 #2
I did not understand the part where you say "static means that method is not a part of the class"
And moreover If I do
Expand|Select|Wrap|Line Numbers
  1. class B{
  2. public static void out () {}
  3. }
  4. class D extends B {
  5. public static void out() {}
  6. }
  7. class I{
  8. public static void main(String[] argv) {
  9. B obj = new D();
  10. obj.out; //out of B is called but if u do something like
  11.  
  12. D obj1 = new D();
  13. obj1.out; //out of D is called
  14. }
  15.  
Is it not supposed that all the functions of java are kind of virtual functions?
Oct 25 '06 #3
r035198x
13,262 8TB
I did not understand the part where you say "static means that method is not a part of the class"
And moreover If I do
Expand|Select|Wrap|Line Numbers
  1. class B{
  2. public static void out () {}
  3. }
  4. class D extends B {
  5. public static void out() {}
  6. }
  7. class I{
  8. public static void main(String[] argv) {
  9. B obj = new D();
  10. obj.out; //out of B is called but if u do something like
  11.  
  12. D obj1 = new D();
  13. obj1.out; //out of D is called
  14. }
  15.  
Is it not supposed that all the functions of java are kind of virtual functions?
A static method is independent of the design of the class eg the main method inside any class has nothing to do with the design of that class. That is why you do not need to create objects to be able to call static methods. eg for your code above, obj.out can be (more appropriately) replaced by B.out.
That is why out of B is called when you do
Expand|Select|Wrap|Line Numbers
  1. B obj = new D();
  2. obj.out;
. B obj says that obj is a B. Any static calls on obj call methods in class B.

about that virtual function thing, I can only say that sometimes it is dangerous to carry over constructs of one language into another.
Oct 25 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.