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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:52:58+00:00 2026-05-13T15:52:58+00:00

I have a Parent.java class and 4 child classes as Child1.java , Child2.java and

  • 0

I have a Parent.java class and 4 child classes as Child1.java, Child2.java and so on.

There are two methods

  • m1()
  • m2()

and one field

  • f1

Field f1 has different values based on the child class.

Method m1 has the common implementation so I have put it in Parent.java class. It also refers method m2.

Method m2 has common implemtation but it process field f1 which is different for all the child classes.

So my questions are as follows:

Q1. Should I place the field f1 in the parent class and let all the child classes inherit them and initialize them in their own constructors or Should I make a field f1 for all the child classes.

Q2. As the method m2 has common implementation but process field f1 which doesn’t have same value for every child class, so should I place it in parent class or child class.

Q3. If I should place the method m2 in parent class, the there is one problem that method m1 (which have common implementation) refer method m2, so would it create any problem?

  • 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-13T15:52:58+00:00Added an answer on May 13, 2026 at 3:52 pm

    Place m2 and f1 in the parent class if m2’s implementation is the same for all classes. If there’s a specific part for each child class that can be run after the common part – separate it and place it in the child classes while calling super.m2(). Set f1 in each child class’s constructor.

    The result will look something like this:

    public abstract class parent {
        private int field = 0;
    
        public parent(int f) {
             field = f;
        }
    
    
        public void m1() { /* m1's implementation */ }
        public void m2() { /* m2's common implementation */ }
    }
    
    public class child1 {
        public child1() {
            super(1);
        }
    
        @Override
        public void m2() { super.m2() /* m2's child1 implementation */ }
    }
    
    public class child2 {
        public child2() {
            super(2);
        }
    
        @Override
        public void m2() { super.m2() /* m2's child2 implementation */ }
    }
    

    This should allow you to push the maximum amount of code as far back in the hierarchy as it can go. Least code duplication.

    Edited to fix trying to access a private member from a child class. Changed to setting it using the constructor.

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

Sidebar

Related Questions

In Java, I have parent class P and a child class C. In an
I have two classes in an owned one-to-many relationship. The parent is Map, and
I have two classes, Parent and Child. The code for the classes are like
I have the following two classes: Claim (parent) and ClaimInsurance (child). They are as
i have a JTree in Java with different parent nodes such as computers, gaming
If you have Parent has_many :children Child Is there any reason a foreign key
I have a parent class which contains a child object. I am using set
If I have a parent-child that defines some method .foo() like this: class Parent
Basically, I have two classes : - MyActivity.java - OtherClass.java Overview of MyActivity.java :
Is it possible to compile java class child of parent class, that invoking method

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.