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

The Archive Base Latest Questions

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

Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return

  • 0

Consider the following code in Python:

class A(object):
    CLASS_ATTRIBUTE = 42

    def f(self):
        return "CLASS_ATTRIBUTE: %d" % self.CLASS_ATTRIBUTE

class B(A):
    CLASS_ATTRIBUTE = 44

Now A().f() and B().f() return "CLASS_ATTRIBUTE: 42" and "CLASS_ATTRIBUTE: 44" respectively.

How can I achieve a similar effect in Java? I want a CLASS_ATTRIBUTE field to be initialized statically and redefined in the inherited class but the f method should be only defined in the base class.

  • 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-17T18:48:35+00:00Added an answer on May 17, 2026 at 6:48 pm

    I’m not sure if you meant “statically” literally or not, but here’s a brief example of how inheritance at it’s most basic form looks in Java. Note that using a getter method to access the variable is a better idea for several reasons — this is just an example.

    public class Dog {
      protected String whatISay = "Woof!";
      public void speak(){
        System.out.println(whatISay);
      }
    }
    
    
    public class Poodle extends Dog {
      public Poodle(){
        whatISay = "Yap!";
      }
    }
    
    
    public class Main {
      public static void main(String[] args){
        Poodle fluffy = new Poodle();
        fluffy.speak();
        Dog dog = new Dog();
        dog.speak();
      }
    }
    

    Yap!
    Woof!

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

Sidebar

Related Questions

Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
Consider the following code: object foo { trait Bar[Q[_]] implicit object OptionBar extends Bar[Option]
Consider the following code in C: for(int i=0; i<10 && some_condition; ++i){ do_something(); }
Consider following code : <p style=margin: 30px 0; padding: 0;>Some text some text some
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)
Consider the following code: try: raise Exception(a) except: try: raise Exception(b) finally: raise This
I would like to use with on an object that uses __getattr__ to redirect
Suppose I have classes Animal , Cat and Dog . Cat and Dog are
I have to process loads of images. First, I need to check if the

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.