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

  • Home
  • SEARCH
  • 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 7003185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:01:59+00:00 2026-05-27T21:01:59+00:00

Wikipedia defines virtual methods as: In object-oriented programming, a virtual function or a virtual

  • 0

Wikipedia defines virtual methods as:

In object-oriented programming, a virtual function or a virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature [to provide a polymorphic behavior].

According to the definition, every non-static method in Java is by default virtual except final and private methods. The method which cannot be inherited for polymorphic behavior is not a virtual method.

Static methods in Java can never be overridden; hence, it is meaningless to declare a static method as final in Java because static methods themselves behave just like final methods. They can simply be hidden in sub classes by the methods with the same signature. It is evidently so because static methods can never have polymorphic behavior: a method which is overridden must achieve polymorphism, which is not the case with static methods.

From the preceding paragraph, an important conclusion can be driven. All the methods in C++ are by default static because no methods in C++ can behave polymorphically until and unless they are explicitly declared virtual in the super class. In contrast, all the methods in Java except final, static and private methods are by default virtual because they have polymorphic behavior by default (there’s no need to explicitly declare methods as virtual in Java and, consequently, Java has no keyword like “virtual”).

Now, let’s demonstrate that instance variables (static too) cannot behave polymorphically from the following simple example in Java.

class Super
{
    public int a=5;
    public int show()
    {
        System.out.print("Super method called a = ");
        return a;
    }
}

final class Child extends Super
{
    public int a=6;

    @Override
    public int show()
    {
        System.out.print("Child method called a = ");
        return a;
    }
}

final public class Main
{
    public static void main(String...args)
    {
        Super s = new Child();
        Child c = new Child();

        System.out.println("s.a = "+s.a);
        System.out.println("c.a = "+c.a);        

        System.out.println(s.show());
        System.out.println(c.show());
    }
}

The output produced by the above code snippet is as follows.

s.a = 5
c.a = 6
Child method called a = 6
Child method called a = 6

In this example, both calls s.show() and c.show() made to the show() method through the variables of type Super and of type Child, respectively, invoke the show() method in the Child class. This means that the show() method in the Child class overrides the show() method in the Super class because both of them have the identical signature.

This, however, cannot be applied to the instance variable a declared in both the classes. In this case, s.a would refer to a in the Super class and display 5 and c.a would refer to a in the Child class and display 6 means that a in the Child class just hides (and doesn’t override as happened to non-static methods) a in the Super class.

After this long discussion, there is only one question. Why are instance variables (and the rest too) not overridden? What were the special reasons to implement such a mechanism? Would have there been any advantages or disadvantages, if they had been overridden?

  • 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-27T21:02:00+00:00Added an answer on May 27, 2026 at 9:02 pm

    I think that the purpose of overriding is changing functionality without changing the signature. This is relevant for methods only: method may have the same signature but have different behavior. Fields have only “signature” that is also limited to there type and name. They do not have behavior, so nothing can be overridden.

    Other reason to inability to “override” fields is that fields are typically private (or should be private), so they are actually the gory details of the class implementation. Methods however represent the external interface of the class. This external interface should support polymorphous to make it easier to change functionality of modules without changing the relationships between modules.

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

Sidebar

Related Questions

Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia
Wikipedia has this to say: Total functional programming (also known as strong functional programming,
Wikipedia states: In practice, explicit right outer joins are rarely used, since they can
Wikipedia says: A programming language is a machine-readable artificial language designed to express computations
Wikipedia defines reflection as follows: In computer science, reflection is the process by which
Wikipedia states: A type can be made impossible to allocate with operator new: struct
From Wikipedia : Generic programming is a style of computer programming in which algorithms
Being asked to describe what a virtual function is seems to be one of
I have a QVector3D which defines an object's velocity in local coordinate system (=
I am attempting to define a function in Maple that defines the Taylor Series

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.