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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:27:22+00:00 2026-05-23T02:27:22+00:00

I have several classes that extend others. They both have a common method result

  • 0

I have several classes that extend others. They both have a common method result. If I have an instance of foobarbaz, is it possible to call its parent’s/grandparent’s result method?

public class Foo {
    protected int resultA;
    public void calc(){ resultA=...} 
    public void result(){  return resultA;      }
}

public class Foobar extends Foo{
   protected int resultA;
   public void calc(){
       super.calc();
       resultB=...;
   } 
   public void result(){  return resultB;      }
}

public class Foobarbaz extends Foobar{
   protected int resultA;
   public void calc(){
       super.calc();
       resultC=...;
   }
   public void result(){  return resultC;      }
}

The problem I’m trying to solve is that each class does some extra calculation, besides the one of its parent. If user wants results from all 3 objects, the CalculationManager knows only Foobarbaz needs to be inited and calculated. Then it returns an reference to Foobarbaz to whoever is asking for Foo, because Foobarbaz will have a result for Foo as well.

Something like:

CalculationManager.add(Foo,Foobar,Foobarbaz);
//The following 3 calls return the same reference to a Foobarbaz object 
Foo       res1=CalculationManager.get(Foo);
Foobar    res2=CalculationManager.get(Foobar);
Foobarbaz res3=CalculationManager.get(Foobarbaz);
CalculationManager.doCalc();
//Iterate over each object to get result with the same method .result()
res1.result();        //---> resultA 
res2.result();     //---> resultB 
res3.result();  //---> resultC 
  • 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-23T02:27:23+00:00Added an answer on May 23, 2026 at 2:27 am

    Design Principle: Favor composition over inheritance

    Get a copy of the Head First Design Patterns book and read about Strategy, Template and maybe also Factory patterns. Other patterns discussed in there will certainly come in handy elsewhere in this project or projects to come.

    Possible scenario:

    • have the Foo, FooBar and FooBarBaz classes extend from an abstract class AbstractFoo
    • give AbstractFoo an attribute of type Calculator myCalc (and getter and setter to go with that)
    • make Calculator an interface with method doCalc() to be implemented by classes that implement this interface
    • create all possible Calculator implementations (CalculatorX, CalculatorY, ..) that implement their own version of doCalc()
    • give AbstractFoo a public int calc() method that calls on the myCalc.doCalc() method to get the results you need

    At the point where you worry about the different Calculator implementations have common code you can make use of the Template pattern or have them extend from a BaseCalculator etc.

    This should keep you busy for some time but if you do it right and get the hang of it you’ll find that using patterns like this helps you in many programming situations. Hence the name patterns, I guess 😉

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

Sidebar

Related Questions

I have several classes that conceptually belong to one tier. They have no common
I have several classes that extend C and I would need a method that
I have a scenario where I have several classes that extend from a common
I have a block of code that basically intializes several classes, but they are
I have a base class with several derived classes that extend it. I want
In our project, we're using gtkmm and we have several classes that extend Gtk::Window
This is for a web project so i have several classes that inherit from
If I have interface IFoo, and have several classes that implement it, what is
I have several service classes that have static methods and offer a service to
I have several serializable classes that were compiled without specifying a serialVersionUID. I now

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.