Explain Codes LogoExplain Codes Logo

Is there a way to override class variables in Java?

java
encapsulation
inheritance
design-patterns
Nikita BarsukovbyNikita Barsukov·Feb 25, 2025
TLDR

In Java, you cannot override static variables, but you can shadow them. This involves defining a new variable with the same name in a subclass. The main effect of this is hiding the superclass's variable when it's referenced from the subclass.

class Parent { static String familyName = "Johnson"; } class Child extends Parent { static String familyName = "Smith"; // Johnson family legend says an ancient curse prevents name change } System.out.println(Parent.familyName); // Will print "Johnson" System.out.println(Child.familyName); // Will print "Smith"

Remember, these shadowed variables don't affect the original variable in the superclass. Instead, they exist independently in their own realms.

Class variables and inheritance: Key points

Understanding hiding vs overriding

In Java, variables (fields) can be hidden while methods can be overridden. Grasping this distinction is crucial when dealing with inheritance hierarchies:

  1. Hiding: This involves fields. Similar names can be declared in a subclass.
  2. Overriding: This applies to methods and allows a subclass to provide a specific implementation for a method that exists in a superclass.

Subtle art of hiding

A static variable might be hidden, but the initial variable in the superclass still retains its value. It's unaffected by changes in the subclass. To modify a class variable in a subclass, use a constructor or static block:

class Parent { protected static String familyName = "Johnson"; } class Child extends Parent { static { familyName = "Smith"; // Every child wants to rebel at some point! } } System.out.println(Parent.familyName); // "Johnson" System.out.println(Child.familyName); // "Smith"

Using getters for access control

Rather than accessing static variables directly, it's common practice to use methods for encapsulation. Protected getter methods offer a way to provide values that are specifically related to subclasses:

class Parent { protected static String familyName = "Johnson"; public static String getFamilyName() { return familyName; } } class Child extends Parent { private static final String familyName = "Smith"; // Mad lad did it! public static String getFamilyName() { return familyName; } }

Enhancing code quality with encapsulation and final

Encapsulation is a key principle of object-oriented programming. It enhances code maintainability and control. Using the final keyword prevents reassignment of variables, ensuring they are immutable:

class History { public static final String FIRST_MAN_ON_MOON = "Neil Armstrong"; // NASA tried to change it to Buzz Lightyear }

Depth into the topic: Design patterns, advanced concepts

Interface-led design

Interfaces can wrap encapsulated behavior and encourage optionality over static variables. They help define a set of common behaviors for classes to implement:

interface Salutations { void greet(); } class Parent implements Salutations { public void greet() { System.out.println("Hello, I'm your father."); // Vader approves } } class Child extends Parent implements Salutations { public void greet() { System.out.println("No."; // Classic Skywalker reply } }

Expanding the abstraction

Abstract classes are fundamental to define a common skeletal structure for subclasses. While complying with the standards of the abstract class, subclasses can manage their static variables and methods according to their needs.

Static Variables Expression

It's critical to understand that static variables are associated with the class they are defined in, not with instances of that class. Therefore, overriding doesn't really make sense in this context:

class Parent { static void sayHello() { System.out.println("Hello Child."); // Father of the year } } class Child extends Parent { static void sayHello() { System.out.println("Hello Father."); // Will Child get end-of-the-year award? } }

Remember, the sayHello method from the Child class shadows Parent's version, they don't behave polymorphically.