static inheritance: is it possible? Are there better solutions? - java

Consider this example (warning-very bad code):
public abstract class A {
static float foo;
public static void loadfoo(float incomingfoo) {
foo = incomingfoo;
}
public static void displayfoo() {
System.out.println("your foo is" +foo);
}
}
Class B extends Class A
public class B extends A {
static float foo;
//#Override (overide is not allowed for static methods. dis is a problem...)
public static void loadfoo(float incomingfoo){
foo = incomingfoo;
}
}
Class C is pretty much the same as B
public class C extends A {
static float foo;
//#Override
public static void loadfoo(float incomingfoo) {
//I would like a different static variable loaded into this class using this method
foo = incomingfoo;
}
}
finally the main Class runs the thing
public class Main {
public static void main(String whatever[]){
B.loadfoo(5);
C.loadfoo(8);
B.displayfoo();
C.displayfoo();
}
}
so the output of this is :
your foo is0.0
your foo is0.0
and I am aware this is because the displayfoo class reference the static foo in Class A, so please disregard this. I assume I have now been specific enough about describing my problem and goal. solutions anyone?
Edit: I feel like an idiot I completely forgot to actually state what I wanted to accomplish, but really all I want is for B and C to have there own static variables loaded into them without altering A's variable, which should be the default.

It looks like you need static access to two stateful objects with the same structure. In this case, an enum might be a solution:
public enum A {
B, C;
private float foo;
// getter and (optional) setter for foo here
public void displayFoo() { System.out.println("This foo is " + foo); }
}
This way you can still access your object statically, but don't need to duplicate anything else:
A.B.setFoo(5);
A.C.setFoo(8);
A.B.displayFoo(); // 5
A.C.displayFoo(); // 8
If you then need a static default, I would make it a method on A:
enum A {
A getDefault() { return A.B; }
}
A.getDefault().displayFoo();

It seems that first you want to load the values using loadfoo to foo and then display the value of that foo using the displayfoo method. Well, I don't think there is anyway to do it using static methods.You can do this by making displayfoo() method abstract and overriding the same in the subclasses B and C.
Here is the code:
abstract class A {
float foo;
public void loadfoo(float incomingfoo){
foo = incomingfoo;
}
public abstract void displayfoo();
}
class B extends A{
#Override
public void loadfoo(float incomingfoo){
foo = incomingfoo;
}
#Override
public void displayfoo(){
System.out.println("foo is " + foo);
}
}
class C extends A{
#Override
public void loadfoo(float incomingfoo){
this.foo = incomingfoo;
}
#Override
public void displayfoo(){
System.out.println("foo is " + foo);
}
}
public class Main {
public static void main(String whatever[]){
B b = new B();
C c = new C();
b.loadfoo(5);
c.loadfoo(5);
b.displayfoo();
c.displayfoo();
}
}
You can also check the same kind of question here.

Static methods should be used by static method access and not by object instance. It's not supposed to be virtual because it's not belong to the object.
If you call B.loadfoo() then a method of B class is called.
If you call C.loadfoo() then a method of C class is called.
You cannot call a static method if it doesn't exist in the class.
There's no point to use static methods if you want to use polimorphism.

Related

implementing static methods for interface

suppose I have an interface:
public interface abcd{
public int a();
public void b();
public void c();
public String d(String h, String j);
}
and I implement it in some class:
public class xyzw implements abcd{
}
but I want the method d() to be static, but I can't do this:
public class xyzw implements abcd{
public static void c(){
//some code
}
}
neither can I do this:
public interface abcd{
public int a();
public void b();
public static void c();
public String d(String h, String j);
}
I wonder if there is something or some workaround or some language construct which allows me to make a method defined by an interface a static method?
You can define a static method in interface, but only with implementation.
public interface A {
public static void b() {
System.out.println("Hi");
}
}
Overriding of static methods is not allowed in Java, because you call it on Class object, not on implementation object.
If you can implement a static method in an interface, but you cannot overwrite it, remember that a static method is referenced by the class itself and not by an instance of it.
To solve your problem maybe you could define an abstract class
No, its not possible and doesn't make any sense. An interface is meant to be implemented by subclasses you can only hava a non-abstract, implemented, static method in an interface. You could not statically call your interface method with
abcd.c()
when it has no implementation. Thats why static elements can not be overridden.
It's not possible to override static methods in java.
However, in the subclass, you can declare static method with the same name and "mask it as" the original method - which is as close as you can get.
interface a {
public static int f() {
return 0;
}
}
interface b extends a {
public static int f() {
return 1;
}
}
System.out.println(a.f());
>> 0
System.out.println(b.f());
>> 1

Passing Object of different classes as an argument to same method

Let's say I have three Classes A,B,C.
All three do the same thing, but in a different way, they differ in efficiency.
All the method names, variable names inside the three classes are same.
class A{
public static int method(){
......
return result;
}
}
class B{
public static method(){
......
return result;
}
}
class C{
public static method(){
......
return result;
}
}
I have test class, which has a method to test the code in the above three classes. Since this testMethod() is common to all the three classes, is there a way to call this method with objects of classes A,B,C ?
class Test{
public static int testMethod(Object ABC)
{
return ABC.method();
}
public static void main(String[] args){
A a = new A();
SOP(testMethod(a));
B b = new B();
SOP(testMethod(b));
C c = new C();
SOP(testMethod(c));
}
}
The only approach I can think of is creating three different methods for each of the classes, like this.
class Test{
public static int testMethodA(A a)
{
return a.method();
}
public static int testMethodB(B b)
{
return b.method();
}
public static int testMethodC(C c)
{
return c.method();
}
public main()
{
//call to each of the three methods
............
}
What is the best approach to this scenario? Basically I want to have only one method that can test all three classes.
Create an interface with the common method for all classes. Then, make each class implement this interface. In your test code, use the interface as parameter type and pass an instance of each class to the method. Note that when you do this, the method to test should not be static.
In code:
public interface MyInterface {
//automatically public
int method();
}
public class A implements MyInterface {
#Override //important
//not static
public int method() {
/* your implementation goes here*/
return ...;
}
}
public class B implements MyInterface {
#Override //important to check method override at compile time
public int method() {
/* your implementation goes here*/
return ...;
}
}
//define any other class...
Then the test:
public class Test {
//using plain naive console app
public static void main(String[] args) {
MyInterface myInterfaceA = new A();
testMethod(myInterfaceA);
MyInterface myInterfaceB = new B();
testMethod(myInterfaceB);
//and more...
}
public static void testMethod(MyInterface myInterface) {
myInterface.method();
}
}
Or if you prefer to use JUnit:
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class MyInterfaceTest {
MyInterface myInterface;
#Test
public void methodUsingAImplementation() {
myInterface = new A();
//code more human-readable and easier to check where the code fails
assertThat(myInterface.method(), equalTo(<expectedValue>));
}
//similar test cases for other implementations
}

Incorrect versions of methods called with java inheritance and interface [duplicate]

This question already has answers here:
Force invocation of base class method
(6 answers)
Closed 9 years ago.
I have a simple hierarchy of inheritance and a single interface. Consider class A which will be extended by class B. I wish for both classes to implement interface I and for class A to be abstract. In java I have roughly the following:
public interface I {
public double foobar();
public void print();
}
public abstract class A implements I {
#Override public double foobar() { /*return foo*/}
#Override public void print() {
System.out.print(foobar());
}
}
public class B extends A {
#Override public double foobar() { /*return bar*/ }
#Override public void print() {
super.print();
System.out.print(foobar);
}
}
My intent is to, when I instantiate an object of type B and call its print(), have the object of type B print first the foo from class A and then the bar from class B; however, when I compile an execute this code it calls the foobar() from class B in both cases. For example, if foo were to equal 1 and bar to equal 2, the output of the above code would be the following:
2
2
But I would like it to be
1
2
I have tried calling the method with various typecasting but have had no luck. Any suggestions?
Edit: Thank you for the many suggestions and just to clarify, I want each of the potential subclasses of A to implement I and I apologise for not stating that explicitly. Additionally, I currently have working code by adding an additional method in A privateFooBar() which is called by both A's print and A's foobar; however, I still wonder if there are more elegant ways to accomplish this task.
foobar() is overridden in class B. So every time it's called on an instance of class B, the class B version of the method is called. That's what polymorphism is all about.
If you want the foobar() method in A to always return A's version of foobar, and don't want methods in subclasses to change that, foobar() should be made final in A.
Or you could delegate to a private method in A:
public abstract class A implements I {
#Override
public double foobar() {
return privateFoobar();
}
private double privateFoobar() {
/* return foo */
}
#Override public void print() {
System.out.print(privateFoobar());
}
}
public class B extends A {
#Override public double foobar() { /*return bar*/ }
#Override public void print() {
super.print();
System.out.print(foobar());
}
}
In your class A , you are calling foobar(), which is overriden in class B
public abstract class A implements I {
#Override public double foobar() { /*return foo*/}
#Override public void print() {
System.out.print(foobar()); // HERE
}
}
You can either make the method final or private
This one is a round about way to foobar. You can call super.foobar() from B, and adjust the overridden print() method. final or private wont work, because then you cannot override the method. Here's the code:
PS: I changed the return types to String to return "foo" and "bar" :)
public class P4 {
public static void main (String[] args) {
new B().print();
System.out.println();
}}
interface I {
public String foobar();
public void print();
}
abstract class A implements I {
#Override public String foobar() { return "foo";}
#Override public void print() {
System.out.print(foobar());
}
}
class B extends A {
#Override public String foobar() { return "bar"; }
#Override public void print() {
System.out.print(super.foobar());
super.print();
}
}
As Class B Override the foobar method of Class A Class B always call the foobar of Class B version.
If you want that Class B will change method behavior of so print that it will print value of foo and bar both without calling super.print().
Define new method for bar in class B instead of Overriding method of Class A.
public class B extends A {
public double bar() { /*return bar*/ }
#Override public void print() {
System.out.print(bar());
System.out.print(foobar());
}
}
As you are changing the print() method it is right to Override print() and not foobar() method.

Inheritance and Private Methods

Given the following block of code:
public class Trial {
public static void main (String[] args){
B obj = new B();
obj.doMethod(); #prints "From A".
}
}
class A {
private void method(){System.out.print("from A");}
public void doMethod(){method();}
}
class B extends A {
public void method(){System.out.print("from B");}
public void doMethod(){super.doMethod();}
}
It turns out that the method() from class A is invoked. Why is this?
You explicitly implement it that way. super calls method from base class which is A
public void doMethod(){super.doMethod();}
So the method chaining is like this:
B.doMethod() -> A.doMethod() -> A.method() -> "from A"
I think your question is if in class A private void method(){System.out.print("from A");} is private then why is printing "from A" in class B.
Answer is very simple you can't call method() of A class form any other class .But you can call it with object of its own.
when you calls super.doMethod(); then its function of super and method() is its own method so it can call it.
Because, see below:
class B extends A {
public void method(){System.out.print("from B");}
public void doMethod(){super.doMethod();}
}
Here in Class B's doMethod() you're invoiking Class A's doMethod() using super.doMethod(). So obviously it's printing Class A's doMethod().
You call the doMethod with super keyword. It's means it will call parent implementation
More on super keyword
Your code gives simple object creation (B obj = new B();) and a call using super. Super is used like other people mentioned for parent class. Things could have been different if you try something like (A obj = new B();), which is more interesting.
method() in class A is private and private methods can't be overriden. And when overriding it's better to use #Override annotion.
class B extends A {
#Override
public void method(){System.out.print("from B");} // Compile error
}
A similar thing happens, if you change the method to a static method.
class A {
public static void method(){System.out.print("from A");}
}
class B extends A {
public static void method(){System.out.print("from B");}
}

Inheriting static variable from abstract class

I have half a dozen classes which all extend the same abstract class. The abstract class has a static variable pointing to some JNI code that I only want to load once per instantiation of the classes.
From what I understand this results in exactly one instance of this static variable being instantiated, but what I want is for each of the extending classes to have their own static instance of the variable that is unique for the given child class. I want to write some code in my abstract class that modifies and/or releases the abstract class. Is it possible to do both of these things at once?
So as an example can I write an abstract class bar with an variable foo and a printFoo method which prints the content of foo. Then I instantiate in order fooBar1, fooBar2, and fooBar3 which each extend the bar class and initialize foo to different values in static blocks. If I call foobar1.printFoo I want to print the static value of foo initialized by fooBar1 constructor.
Can this be done in java?
You can approximate it, but you will need separate static variables for each subclass, to stop subclasses overwriting each others values. It's easiest to abstract this via a getter getFoo so that each subclass fetches the foo from the right place.
Something like this
abstract class Bar
{
// you don't have to have this in the base class
// - you could leave out the variable and make
// getFoo() abstract.
static private String foo;
String getFoo() {
return foo;
}
public void printFoo() {
System.out.print(getFoo());
}
}
class Foo1 extends Bar
{
static final String foo1;
public String getFoo() {
return foo1; // return our foo1 value
}
public Foo1() {
foo1 = "myfoo1";
}
}
class Foo2 extends Foo1
{
static final String foo2;
public String getFoo() {
return foo2; // return our foo2 value
}
public Foo2() {
foo2 = "myfoo2";
}
}
I have a similar problem. Looks like Java can't isolate static members (attributes). I ended up adding an abstract method instead of the attribute:
public abstract class Abs {
public void printX() {
System.out.println("For " + this.getClass() + " x=" + getX());
}
protected abstract Integer getX();
}
public class A extends Abs {
protected static Integer x = 1;
#Override
protected Integer getX() {
return x;
}
}
public class B extends Abs {
protected static Integer x = 2;
#Override
protected Integer getX() {
return x;
}
}
public class test {
public static void main(String args[]) {
Abs a = new A();
a.printX();
Abs b = new B();
b.printX();
Abs c = new A();
a.printX();
b.printX();
c.printX();
}
}

Categories

Resources