The Static Methods in Java Subclass

The Static Methods in Java Subclass thumbnail
Subclasses can extend and override parent class methods.

Static methods throw many novice Java programmers into hours of fighting with source code that is giving them errors. These problems can be even stickier when Java developers integrate them into their class files, and especially problematic when developers use them in their subclasses. However, a proper understanding of the principles of inheritance can clear up much of this confusion.

  1. Classes

    • Object-oriented programming is premised on code structures known as "classes." When a programmer writes a class, he is effectively creating a template he can use to create multiple instances of that code structure, or objects. For example, a game programmer who is coding a game can write a class that contains all the code to create a functional character. Instead of rewriting this code repeatedly to create multiple characters, he can simply create multiple instances of the single class he wrote.

    Sub Classes

    • Java programmers can further specialize the classes they write with subclasses. These are effectively extensions to a parent class. For example, a game programmer can write a class that contains the basic mechanisms for making characters move around and interact. She can then create subclasses -- which automatically inherit all the code of their parent class without her having to retype everything -- that contain specialized code to make different types of characters such as allies, enemies and bosses.

    Static and Instance Methods

    • Individual functions within an object are called methods. There are two kinds of methods: instance methods and static methods. Each time a programmer creates a new instance of a class, this object is an entirely autonomous unit. It has its own variables whose values are distinct from other instance objects of the same class and its instance methods only affect its own variables. However, static methods within a class are treated differently. Rather than only having access to a specific instance, static methods access data from every instance of a class. Alternatively, if the static method is declared within a subclass, it has access to data from every instance object of that particular subclass.

    Static Methods and Overriding

    • When subclasses extend their parent classes, programmers have the option of "overriding" methods in the parent class with methods from the subclass. By writing a method with the same name in a subclass that exists within the parent class it extends, calling that method in your program will cause Java to use the method code from the subclass instead of the parent class. However, this does not work with static methods. If a programmer tries to override a static method from a parent class in a subclass, his program will generate an error message at compile time.

Related Searches:

References

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

You May Also Like

Related Ads

Featured