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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:54:42+00:00 2026-05-15T04:54:42+00:00

Suppose I have two classes defined the following way: public class A { public

  • 0

Suppose I have two classes defined the following way:

public class A {

   public A() {
      foo();
   }

   public void foo() {
      System.out.println("A");
   }
}

public class B extends A {

   private String bar;

   public B() {
      bar = "bar";
   }

   @Override
   public void foo() {
      System.out.println(bar);
   }

}

And then i instantiate B the following way:

A test = new B();

So why can’t the compiler respectively the IDE warn me that there will be a NullPointer in the foo method of B? That wouldn’t be to difficult to check and sometimes very useful.

  • 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-15T04:54:43+00:00Added an answer on May 15, 2026 at 4:54 am

    While this is a design error, it’s not a grammatical error.

    Here’s some quote from Effective Java 2nd Edition, Item 17: Design and document for inheritance, or else prohibit it:

    There are a few more restrictions that a class must obey to allow inheritance. Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will be invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.

    An obedient compiler would let it compile just fine, since it is linguistically legal. Fortunately, code analysis tools can be used to find these design errors, e.g. findbugs:

    UR: Uninitialized read of field method called from constructor of superclass (UR_UNINIT_READ_CALLED_FROM_SUPER_CONSTRUCTOR)

    This method is invoked in the constructor of of the superclass. At this point, the fields of the class have not yet initialized.

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

Sidebar

Related Questions

Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I have two classes: class A { public: int i; }; class B :
Suppose I have two classes like so: class Parent def say I am a
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose I have the following two strings containing regular expressions. How do I coalesce
Suppose I have a database table with two fields, foo and bar. Neither of
I have a little C# problem. I have two classes ClassA and ClassB defined
Suppose that I have two data types Foo and Bar. Foo has fields x
Suppose I have two classed Base and Derived , i.e.: #include <iostream> class Base
So, I have a little problem here. Suppose I have: public class Repository<TEntity> where

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.