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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:07:34+00:00 2026-05-23T18:07:34+00:00

When you override a method you must keep the method’s signature and can’t reduce

  • 0

When you override a method you must keep the method’s signature and can’t reduce its visibility. Now I tried what happends when I do this with attributes. I was surprised – IT WORKS! Look yourself:

public class A {

    public Integer myAttribute;

}

public class B extends A {

    public String myAttribute;

}

public class Main {

        public static void main(String[] args) {
        B b = new B();
        b.myAttribute = "myString";
        ((A) b).myAttribute = 1337;
        System.out.println(b.myAttribute);
        System.out.println(((A)b).myAttribute);
    }

}

So it is possible to write an attribute in a subclass with the same attibute name as in the superclass, but you can use different visibility (modifier) and type. So I would say that the attributes in superclass and subclass are almost completely independend from each other.

Now if you really use the same attribute name in superclass and subclass you effectively hide the superclass’s attribute. When accessing the attribute using the subclass, you get the subclass’s attribute. But the superclass’s attribute is there, too! You have to perform a cast to access the superclass’s attribute from outside. From inside the “super” keyword should be usefull.

Question 1:
Now basically you have two different attributes with the same name. Doesn’t this break LSP?

Why I came to this detail is this:
I am experimenting with an self written persistence framework. I want to read the complete internal state of an object and persist that state. I read all attribute’s values via reflexion, starting at the subtype and traversing over the superclasses.
Now if there are two attributes with the same name in superclass and subclass I have to remember the class which declares an attribute to be able to map the attributes values to the right attributes and restore an object’s state.

Question 2:
Any other ideas how to deal with this detail?
Question 3:
How do other persistence frameworks handle this detail?

I never saw this detail in use. Maybe writing two attributes with the same name is a bit ugly, but it is possible and any persistence framework may be confronted with it. Maybe there are situations where this technique might be usefull.

Thans in advance.

  • 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-23T18:07:34+00:00Added an answer on May 23, 2026 at 6:07 pm

    ORMs usually use the javabeans standard which defines “properties”. Properties are defined by a reader and/or writer method rather than the field they read/write. In 99% of the cases properties are field+getter&setter, but it need not be the case. And the ORM reads these properties, and detects only the fields that have getters&setters. Since methods are overridden rather than shadowed, the problem is gone.

    This is more a problem of encapsulation than a violation of LSP. Field access is not affected by polymorphism, so if you have Foo foo = new FooSubclas();foo.fieldthe value of theFoo` field will be taken, which means the behaviour won’t change.

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

Sidebar

Related Questions

Should a method that implements an interface method be annotated with @Override ? The
I am trying to override the DataGridViewTextBoxCell's paint method in a derived class so
Which method View() is called last in the System.Web.Mvc.Controller? Or should I override each
I have a base class with a virtual method, and multiple subclasses that override
I'm trying to override Ant compiler attributes via the command line so that all
We'd like to override DataGridView's default behavior when using a mouse wheel with this
I tried to override the settings in the default stlyesheet that comes with the
Is it possible to override default value of WebClientProtocol.Timeout property via web.config? <httpRuntime executionTimeout=500
I want to override the JSON MIME type (application/json) in Rails to (text/x-json). I
I want to override OnMouseClick and OnMouseDoubleClick and perform different actions depending on which

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.