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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:41:32+00:00 2026-05-13T06:41:32+00:00

class BaseClass { protected int filed = 1; public void method() { System.out.println(+ BaseClass

  • 0
class BaseClass {
    protected int filed = 1;
    public void method() {
        System.out.println("+ BaseClass method");
    }
}

class DerivedClass extends BaseClass {
    private int filed = 2;
    public void method() { 
        System.out.println("+ DerivedClass method");
    }

    public void accessFiled() {
        System.out.println("DerivedClass: default filed = " + filed); // output 1
        System.out.println("DerivedClass: upcasting filed = " + ((BaseClass)this).filed); // output 2
    }

    public void accessMethod() {
        System.out.println("DerivedClass: default method");
        method(); // output "+ DerivedClass method"
        System.out.println("DerivedClass: upcasting method");
        ((BaseClass)this).method(); // expecting "+ BaseClass method" but "+ DerivedClass method"
    }
}

Why the access to filed(data member) and method differs?Could you explain it to me on both language design and implementation details aspects?

thanks.

  • 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-13T06:41:32+00:00Added an answer on May 13, 2026 at 6:41 am

    This happens because you can only override methods, not fields. In DerivedClass your hiding the variable filed declared in the BaseClass. An instance of DerivedClass really has 2 fields called filed and you can access both with the appropriate cast. It wouldn’t make much sense being able to override fields… Only behavior.

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

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, you're taking an int and are casting it to… May 16, 2026 at 3:27 pm
  • Editorial Team
    Editorial Team added an answer All parameter passing in Java is by value. Here is… May 16, 2026 at 3:27 pm
  • Editorial Team
    Editorial Team added an answer The code you posted essentially lumps all of your rotations… May 16, 2026 at 3:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I wrote a base class which defined many protected methods . Those methods are
I would like to use a base message class like: [Serializable] public abstract class
I'm writing an attribute where I want to access a method on the class
I'm using the class listed below to create a UserControl wrapping a ComboBox that
I'm trying to create a C++ class, with a templated superclass. The idea being,
I have an abstract base class and I want to declare a field or
I have this base class package sevengames.miranda.front.res { import flash.display.MovieClip; import flash.text.TextField; public class
Suppose i have the code snippet as follows : ( clarification purpose/not well formed
I've been googling and reading about this and didn't come up with an answer
Kind of theoretical question. Quite long so feel free to skip if you are

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.