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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:46:56+00:00 2026-05-26T16:46:56+00:00

If I have 2 classes, one being the parent with the following: public class

  • 0

If I have 2 classes, one being the parent with the following:

public class Parent {
    ...
    public void method1() {
        method2();
    }

    public void method2() {
    }
}

And then in the subclass

public class Child extends Parent {
    ...
    public void method2() {
        ...
    }
}

If I run the following code:

Child c = new Child();
c.method1();

Which version of method2 gets called?

  • 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-26T16:46:57+00:00Added an answer on May 26, 2026 at 4:46 pm

    All methods are virtual in Java, which means that it is the Child.method2 that will be called (even if the call is done from the context of Parent).

    If the correctness of Parent.method1 relies on the implementation of method2, you should design it differently:

    public class Parent {
        ...
        public void method1() {
            method2impl();
        }
    
        public void method2() {
            method2impl();
        }
    
        // make it private or final.
        public final method2impl() {
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes like this one: class SomeObject { public function __construct($param1, $param2) {
I have two classes. One is Order: public class Order { public virtual int
I have two classes, and want to include a static instance of one class
I have two classes: Parent and Child with Child: belongs_to :parent and Parent has_many
I have two classes. one to run the program and the other for being
Is it possible to have two step definition classes with first being in one
I have code like this: class Base { public: void operator = (const Base&
I have two maven-based eclipse projects, one being a project with common classes and
I have two classes one of which inherits from the other. Im trying to
I have 2 classes where I have one to send commands to a socket,

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.