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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:13:19+00:00 2026-05-26T16:13:19+00:00

Suppose I have a CommonClass class that displays what is common to all others

  • 0

Suppose I have a CommonClass class that displays what is common to all others class files:

public class CommonClass {
   public static void displayFoo() {
      ... // printlns here
      displaySpecific(); // Among all files made common by this method, I
                         // want to display something different among the other files
      ... // printlns here
   }
}

I intend to call that like this:

// Filename: fileA.java

public class FileA {
    public static void myFunc() {
         CommonClass.displayFoo();
         // however, I should have a specific definition
         // for the displaySpecific()
         // method. Should I use interfaces? How should it be structured.
    }
    // displaySpecific method here
}

Another file:

// Filename: fileB.java

public class FileB {
    public static void myFunc() {
         CommonClass.displayFoo();
         // however, I should have a specific definition
         // for the displaySpecific()
         // method. Should I use interfaces? How should it be structured.
    }
    // displaySpecific method here
}

and so on…

Main could be this…

public class MyMain {
   public static void main(String[] args) {
      FilaA.myfunc();
      System.out.println("");
      FileB.myfunc();
      System.out.println("");
      ... and so on...
   }
}

This is the intended output:

Common String Common String Common Common Common Common Common 
Common Common Common Common Common Common Common 
This is File A
Common String Common String Common Common Common Common Common 
Common Common Common Common Common Common Common 

Common String Common String Common Common Common Common Common 
Common Common Common Common Common Common Common 
This is File B
Common String Common String Common Common Common Common Common 
Common Common Common Common Common Common Common 

How should I do that?

  • 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-26T16:13:20+00:00Added an answer on May 26, 2026 at 4:13 pm

    If you need different definitions of displayFoo() per-class then they should be class functions. Your idea of an interface is probably the way you want to go.

    public class FileB implements Displayable {
        public static display() {
            // Implementation-specific display
    // etc.
    

    Then you just call display() on your displayable objects:

    for (Displayable d : displayables) {
        d.display();
    }
    

    In your example, however, you don’t really need an interface or anything else, because you’re calling myfunc() which in turn would call the class’s own display().

    You could create something like this:

    public class DisplayUtil {
        public static void display(FileA f) {
            // etc.
        }
    
        public static void display(FileB f) {
            // etc.
        }
    }
    

    Not a big fan of the pattern, but if you don’t have access to the original source, for example, or they’re final classes (I’m looking at you, java.lang.String), it may be the more-convenient option.

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

Sidebar

Related Questions

Suppose I have a static method of my class that returns an object of
Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have string Name and Image Photo as properties of a class in
Suppose you have a subsystem that does some kind of work. It could be
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and

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.