Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:23:45+00:00 2026-06-12T13:23:45+00:00

I’m trying to get the hang of inheritance in Java and have learnt that

  • 0

I’m trying to get the hang of inheritance in Java and have learnt that when overriding methods (and hiding fields) in sub classes, they can still be accessed from the super class by using the ‘super’ keyword.

What I want to know is, should the ‘super’ keyword be used for non-overridden methods?

Is there any difference (for non-overridden methods / non-hidden fields)?

I’ve put together an example below.

public class Vehicle {
    private int tyreCost;

    public Vehicle(int tyreCost) {
         this.tyreCost = tyreCost;
    }

    public int getTyreCost() {
        return tyreCost;
    }        
}

and

public class Car extends Vehicle {
    private int wheelCount;

    public Vehicle(int tyreCost, int wheelCount) {
        super(tyreCost);
        this.wheelCount = wheelCount;
    }   

    public int getTotalTyreReplacementCost() {
        return getTyreCost() * wheelCount;
    }   
}

Specifically, given that getTyreCost() hasn’t been overridden, should getTotalTyreReplacementCost() use getTyreCost(), or super.getTyreCost() ?

I’m wondering whether super should be used in all instances where fields or methods of the superclass are accessed (to show in the code that you are accessing the superclass), or only in the overridden/hidden ones (so they stand out).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:23:46+00:00Added an answer on June 12, 2026 at 1:23 pm

    Don’t use the super keyword to refer to other methods which aren’t overridden. It makes it confusing for other developers trying to extend your classes.

    Let’s look at some code which does use the super keyword in this way. Here we have 2 classes: Dog and CleverDog:

    /* file Dog.java */
    public static class Dog extends Animal {
    
        private String name;
    
        public Dog(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
    }
    
    /* file CleverDog.java */
    public class CleverDog extends Dog {
    
        public CleverDog(String name) {
             super(name);
        }
    
        public void rollover() {
            System.out.println(super.getName()+" rolls over!");
        }
    
        public void speak() {
            System.out.println(super.getName() + " speaks!");
        }
    
    }
    

    Now, imagine you are a new developer on the project, and you need some specific behavior for a clever dog who is on TV: that dog has to do all its tricks, but should go by its fictitious TV name. To accomplish this, you override the getName(...) method…

    /* file DogOnTv.java */
    public class DogOnTv extends CleverDog {
    
        String fictionalName;
    
        public DogOnTv(String realName, String fictionalName) {
            super(realName);
            fictionalName = fictionalName;
        }
    
        public String getName() {
            return fictionalName;
        }
    
    }
    

    … and fall into a trap set by the original developer and their unusual use of the super keyword!

    The code above isn’t going to work – because in the original CleverDog implementation, getName() is invoked using the super keyword. That means it always invokes Dog.getName() – irrelevant of any overriding. Consequently, when you use your new DogOnTv type…

        System.out.println("Showcasing the Clever Dog!");
        CleverDog showDog = new CleverDog("TugBoat");
        showDog.rollover();
        showDog.speak();
    
        System.out.println("And now the Dog on TV!");
        DogOnTv dogOnTv = new DogOnTv("Pal", "Lassie");
        dogOnTv.rollover();
    

    … you get the wrong output:

    Showcasing the Clever Dog!
    Tugboat rolls over!
    Tugboat speaks!
    
    And now the Dog on TV!
    Pal rolls over!
    Pal speaks!
    

    This is not the usual expected behavior when you override a method, so you should avoid creating this kind of confusion using the super keyword where it doesn’t belong.

    If, however, this is actually the behavior you want, use the final keyword instead – to clearly indicate that the method can’t be overridden:

    /* file CleverDog.java */
    public class CleverDog extends Dog {
    
        public CleverDog(String name) {
             super(name);
        }
    
        public final String getName() { // final so it can't be overridden
            return super.getName();
        }
    
        public void rollover() {
            System.out.println(this.getName()+" rolls over!"); // no `super` keyword
        }
    
        public void speak() {
            System.out.println(this.getName() + " speaks!"); // no `super` keyword
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.