Jump to content

I have a set of 10 methods that will be shared across 4 classes, although with different implementations in each. I also want several subclasses of each of these 4 classes so that I can implement an additional method unique to each subclass.

 

My idea was to create an interface with the 10 shared methods. Then 4 abstract base classes that would fully implement the interface in their own unique way, along with an abstract method to be implemented in each subclass.

 

The problem is that I would like all of the methods to be called by subclass name, requiring static method declarations. But I am constrained to Java 7, so no static or default interface methods.

 

I would appreciate any help as I am new to Java and programming in general.

i5-4690K@4.5 GHz // Asus Z87-Pro // HyperX Fury 8GB DDR3-1600 // Crucial BX100 250GB // Sapphire Nitro R9 390 // EVGA SuperNOVA 750W G2 // Fractal Design Define S // be quiet! Pure Rock & Pure Wings 2 // BenQ XL2730Z // Corsair Vengeance K70 // Logitech G403 Wireless // Sennheiser HD 598 SE

Link to comment
https://linustechtips.com/topic/566946-trouble-creating-java-helper-classes/
Share on other sites

Link to post
Share on other sites

Depends what you want to do, but you can use the Singleton design pattern approach. 

Basically you can create 1 class lets call it "Constants" here you will create static declarations to all your Classes something like this

 

 

public static MyClass1 myclassname = new MyClass1();

public static MyClass2 xxxx2 = new MyClass2();

public static MyClass3 somethingsomething = new MyClass3();

public static MyClass4 class4 = new MyClass4();

 

 

 

 

now they're stored in your "Constants" class if you want to access any methods that these classes holds you can reference them back like this

 

 

 

Constants.class4.method22();

 

 

 

method22 is not static 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×