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 7509955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:04:49+00:00 2026-05-29T23:04:49+00:00

I have a super class: public class SuperClass { public void dosomething() { firstMethod();

  • 0

I have a super class:

public class SuperClass {

    public void dosomething() {
        firstMethod();
        secondMethod();
    }

    public void firstMethod() {
        System.out.println("Super first method");
    }

    public void secondMethod() {
        System.out.println("Super second method");
    }
}

A sub class:

public class SubClass extends SuperClass {

    public void dosomething() {
        super.dosomething();
    }

    public void firstMethod() {
        System.out.println("Sub first method");
    }

    public void secondMethod() {
        System.out.println("Sub second method");
    }
}

A test class:

public static void main(String[] args) {
      SubClass sub = new SubClass();
      sub.dosomething();
      SuperClass sup = new SuperClass();
      sup.dosomething()
}

when I run the test method, I got this:

Sub first method
Sub second method

Can you tell me how this happened? In the sub class dosomething method, I called super.dosomething() and I think the super method will be called, but the override method in sub class was called.

if I do this:
SuperClass superClass = new SuperClass();
superClass.dosomething();

the result is:
Super first method
Super second method

The difference is method invocation place. I think there must be something I don`t know ):

oops!the super reference pointed to subclass in the first example…

like this:


SuperClass sub = new SubClass();
sub.firstMethod();
sub.secondMethod();

  • 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-05-29T23:04:52+00:00Added an answer on May 29, 2026 at 11:04 pm

    Your object on which the methods are invoked is of type SubClass, not SuperClass. Even if you call a method that is only defined in SuperClass, your execution context remains SubClass. So any method that is invoked that is overridden will in fact execute the overridden method.

    The thing to take away from this is that by declaring firstMethod and secondMethod as public, SuperClass is in fact allowing subclasses to override their behaviour. If this is not appropriate, the methods should be private, or final.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a super class like this: class Parent { public: virtual void Function(int
Consider this code: class Test { Test() { System.out.println(In constructor of Superclass); } int
I have this code: #include <iostream> class Super{ public: virtual void showName(); }; class
If subclass in objective-c wants to override a super class's method, does it have
I have a super super (grandparent) abstract bean class calling loadData() method in its
I have wired experience with reflections. At first some sample code: public abstract class
class Super { public void anotherMethod(String s) { retValue(s) } public String retValue(String s)
I have a class that indirectly implements an interface (its superclass or super-duper class
I have a super class with telescoping constructors with a finalize() method. To protect
I have the following code: class SuperClass { public static String getName() { return

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.