Java: Implementing an interfacing by inheriting a class - java

Time-appropriate greetings :)
Working in Java, I have an Interface A. All implementors of this Interface also extend class B, but B does not implement A. In a class where we use an instance of A (referenced as A), it is cast to a B Reference so that we can use a Method defined in class B. It makes sense conceptually that the Method should belong in Interface A too.
Can you think of a reason not to introduce the Method to Interface A, so that we don't have to cast it to B? Should I maybe override the Method in the subclasses and just call the super version, so that it's easier to navigate in the IDE etc?

In a class where we use an instance of A (referenced as A), it is cast to a B Reference so that we can use a Method defined in class B.
So I'm assuming you have this scenario
public void doStuff(A aType){
...
B bType = (B) aType;
...
}
If this is true, can this work?
private <T extends B & A> void example(T type){
type.aStuff();
type.doBStuff();
}
I created the following to test this.
public class Foo{
private static interface A{
void aStuff();
}
private static class B{
public void doBStuff(){
System.out.println("B stuff");
}
}
private static class AB extends B implements A{
public void aStuff(){
System.out.println("A stuff");
}
}
public static void main(String[] args) {
Foo foo = new Foo();
foo.example(new AB());
}
// method "example" already given
}
Gave me
A stuff
B stuff

Why not creating an abstract class which extends B and implements A? Assuming this class would be called C, your other classes would extend C and implement the method required by A, but will provide you with the methods available in B without casting.
I think that moving methods now would not be a good idea, maybe, at most, have B implement A (assuming you have no other classes which you haven't talked about which are dependent on the classes and interfaces you mentioned).

Related

Can we Pass Abstract Class Object as Argument Using Polymorphism?

I have a class with name 'A'. A is an abstract class. And class 'B' extends class 'A'.
And I have another class 'C'. In class 'C' there's a function with name show().
I want to pass an object of class 'A' which is abstract. Is it
possible?
Or
Can we do this using Polymorphism.
If yes! then How?
I want to pass an object of class 'A' which is abstract. Is it possible?
Yes, it is. The following is valid:
abstract class A {}
class B extends A {}
class C {
public void show(A a) {}
}
Even though A is abstract, you can receive parameters of type A (which, in this case, would be objects of type B).
You cannot really pass an object of class A, though, since A is abstract and cannot be instantiated.
Can we do this using Polymorphism.
The above example actually already used polymorphism (subtyping).
https://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping
Pretty much the same as above answer, just elaborated with code. Naive way of telling, you cannot have abstract class name next to new operator except in case with array, as in A a[] = new A[10]; where you have still allocate Objects of concrete class for each element in Array.
abstract class A{
abstract void tell();
}
class B extends A{
void tell(){
System.out.println("I am B Telling");
}
}
public class Test{
public static void whoTold(A a)
{
a.tell();
}
public static void main(String[] args){
B b = new B();
whoTold(b);
}
}

How to access sub class methods when sub class object is instantiated from abstact class

I have an abstract class called A and class that extends from it called B. In the A class I have a concrete implementation of a method and also I have abstract methods. In class B I have overridden all abstract methods and a method of its own.
public abstract A{
public boolean myConcreteMethod(){
//implementation
}
public abstract void abstractMethod();
}
public class B extends A{
#Override
public void abstractMethod(){
//implementation
}
public String aMethodBelongsToB(){
//implementation
}
}
Now when I instantiate an object of B like following:
A test = new B();
I am unable to access
test.aMethodBelongsToB();
Am I doing something wrong here? Any help would be greatly appreciated.
You don't have the B reference.so, you cannot call it's methods.You have to make it
B test = new B();
Now, B can access methods of A. Since B is A in OOP terminology. IS-A relationship!
When you instantiate class B, you're using a reference variable to the type of A. From this reference, you can only access methods declared in A. That reference can also point to other classes that inherit from A, that would not have the methods declared in B, so the compiler doesn't let you call methods only declared in a subclass from a reference to a superclass.
When you assign a B value to a variable of type A, the Java language says that it is not possible to call the B-specific methods. Either do this:
B test = new B();
test.aMethodBelongsToB();
or this:
A test = new B();
((B) test).aMethodBelongsToB();
Note: since we are casting test to B can now call the B-specific methods ... on the result of the typecast subexpression.
It is always the declared type of the variable/expression that determines what method (signatures) are available to be called. The actual object reference only determines which actual method is called (if there is method overriding). To understand why, consider this:
public class C extends A{
#Override
public void abstractMethod(){
//implementation
}
...
}
and this (which is not valid Java ...):
A test;
if (something) {
test = new B();
} else {
test = new C();
}
test.aMethodBelongsToB(); // INVALID
If that last statement was legal (and it isn't!!) what should happen there if something is false?

If a child class extend Base class Exception

I had a doubt.
Imagine If we have a class A that implements the method
For example
private void methodA(int index) throws Exception, Error {
}
And if we have a Class B that extends the first class A.
My questions is, can class B implement
private void methodA(int index) throws Exception, Error {
}
And which method will be called under which circumstance!!
Thanks
If your methods weren't declared "private", this would just be standard polymorphism. Because they're private, the rules are a bit different. The version in class A can only be called from code that's in class A. The version in class B can only be called from code that's actually written in class B (as opposed to code that class B gets by extending class A).
YES, you can implement the methodA method in class B, but, pay attention, you are not overriding it.
Your method is declared ad private so is not "visible" from extending classes.
If your intention is to make your method overridable, you need to declare it as public.
Just give it a try :)
public class Main {
public static void main(String[] args) {
Base base;
base = new A();
System.out.println(base.doSth());
base = new B();
System.out.println(base.doSth());
}
}
abstract class Base {
public abstract String doSth();
}
class A extends Base {
#Override
public String doSth() {
return "A";
}
}
class B extends A {
#Override
public String doSth() {
return "B";
}
}
I think you wonna override the super-class method, and to do this, the method on sub-class must have the same signature of super-class method.
You can call these methods in following ways:
Suppose test1 is an instance of classA, teste1.methodA(index) will execute the implementation on super-class.
Suppose test2 is an instance of classB, test2.methodA(index) will execute the sub-class method.
In classB you can invoque the super class method (if the method is notprivate), something like :
public class ClassB extends ClassA
{
...
super.methodA(index);
...
}

Java design, how do I avoid passing object down the hierarchy?!? Impossible?

I have a number of classes, please allow me to introduce them and then ask my question at the end:
I have a container class which contains two objects in a composite relationship:
public class Container{
A a;
B b;
public someMethod(){
a.getC().myMethod(b);
}
}
A and B are superclasses (or Interfaces), with subtypes that can also be the type held in the composite relationship.
A contains a member of (interface) type C:
public class A{
C c;
}
public interface C{
public void myMethod(B b);
}
public class D implements C{
public void myMethod(B b){
//This code will modify the state of object b, in class Container.
b.changeState();
}
}
public class E implements C{
public void myMethod(B b){
//This code will modify the state of object b, in class Container.
b.changeState();
}
}
My problem is that I wish to modify the state of object b from a method starting in the container class, which eventually calls code down the hierarchy, to classes D and E- calling myMethod() via dynamic binding. I want to do this because I am going to use polymorphism to run the correct myMethod() (depending on whether the type of object is D or E) and I wish to do this, rather than write IF statements.
So my problem is that it seems very bad continually passing the instance of object b down the class hierarchy to myMethod, so that I can run b-specific code to modify the state of b. Is there anything else I can do to modify b from d and e (collectively known as c)?
I can get this to work using just interfaces but without using generics- but when I added generics i had problems with types and that made me start to think if my whole design was flawed?
EDIT: I could probably do this easily just by using IF statements- but I wanted an elegant solution using polymorphism of classes D and E.
First of all, if I understood your question correctly, no instance of B is being "passed down" in your code. Dynamic dispatch will simply cause the myMethod() implementation in the actual type of a to be called with an instance of B as argument.
While it may be tedious to have to write the argument explicitly every time you implement myMethod(), there's nothing wrong with it.
The alternative is to give each subclass/implementation of A an attribute of type B. In this case, however, you would have to pass your B instance down the chain of constructors to the class that actually has your B attribute.
Your code would become:
public class A{
C c;
public A(C c) {
this.c = c;
}
public interface C{
public void myMethod(B b);
}
public abstract class CC {
protected B b;
public CC(B b) {
this.b = b;
public class D extends CC implements C {
public D(B b) {
super(b);
}
public void myMethod(){
b.changeState();
}
}
public class E extends CC implements C {
public E(B b) {
super(b);
}
public void myMethod(){
b.changeState();
}
}
And then somewhere, e.g. in Container's constructor:
b = new B();
a = new A(new E(b));
You could pass the instance of B to the constructor of E. (or use a setter). That poses issues in itself, but at least it avoids having to pass B down every time you call myMethod(), which now needs no arguments.
e.g.
somewhere inside B
E myE = new E(this);
and, inside E
final B myB;
public E(B myHigherLevelThing) {
this.myB = myHigherLevelThing;
}
public void myMethod() {
myB.changeState();
}
Use the most general interface for the declarations, I'm a little confused about your full hierarchy so there may be room for improvement there...

Java; casting base class to derived class

Why can't I cast a base class instance to a derived class?
For example, if I have a class B which extends a class C, why can't I do this?
B b=(B)(new C());
or this?
C c=new C();
B b=(B)c;
Alright let me be more specific as to what I'm trying to do. Here's what I have:
public class Base(){
protected BaseNode n;
public void foo(BaseNode x){
n.foo(x);
}
}
public class BaseNode(){
public void foo(BaseNode x){...}
}
Now I want to create a new set of classes which extend Base and Basenode, like this:
public class Derived extends Base(){
public void bar(DerivedNode x){
n.bar(x);//problem is here - n doesn't have bar
}
}
public class DerivedNode extends BaseNode(){
public void bar(BaseNode){
...
}
}
So essentially I want to add new functionality to Base and BaseNode by extending them both, and adding a function to both of them. Furthermore, Base and BaseNode should be able to be used on their own.
I'd really like to do this without generics if possible.
Alright so I ended up figuring it out, partly thanks to Maruice Perry's answer.
In my constructor for Base, n is instantiated as a BaseNode. All I had to do was re-instantiate n as a DerivedNode in my derived class in the constructor, and it works perfectly.
because if B extends C, it means B is a C and not C is a B.
rethink what you are trying to do.
The existing answers are fine in terms of an abstract argument, but I'd like to make a more concrete one. Suppose you could do that. Then this code would have to compile and run:
// Hypothetical code
Object object = new Object();
InputStream stream = (InputStream) object; // No exception allowed?
int firstByte = stream.read();
Where exactly would the implementation of the read method come from? It's abstract in InputStream. Where would it get the data from? It simply isn't appropriate to treat a bare java.lang.Object as an InputStream. It's much better for the cast to throw an exception.
In my experience it's tricky to get "parallel class hierarchies" like the one you're describing to work. You may find that generics help, but it can get hairy very quickly.
You need to use the instanceof keyword to check the type of object referenced by n and typecast the object and call the bar() method. Checkout Derived.bar() method bellow
public class Test{
public static void main(String[] args){
DerivedNode dn = new DerivedNode();
Derived d = new Derived(dn);
d.bar( dn );
}
}
class Base{
protected BaseNode n;
public Base(BaseNode _n){
this.n = _n;
}
public void foo(BaseNode x){
n.foo(x);
}
}
class BaseNode{
public void foo(BaseNode x){
System.out.println( "BaseNode foo" );
}
}
class Derived extends Base{
public Derived(BaseNode n){
super(n);
}
public void bar(DerivedNode x){
if( n instanceof DerivedNode ){
// Type cast to DerivedNode to access bar
((DerivedNode)n).bar(x);
}
else {
// Throw exception or what ever
throw new RuntimeException("Invalid Object Type");
}
}
}
class DerivedNode extends BaseNode{
public void bar(BaseNode b){
System.out.println( "DerivedNode bar" );
}
}
You can create a constructor for B that takes C as a parameter.
See this post for ideas to do what you're trying to do.
Base classes shouldn't know anything about classes derived from them, otherwise the problems highlighted above will arise. Downcasting is a 'code smell', and downcasting in the base class to a derived class is particularly 'smelly'. Such designs can lead to difficult to resolve circular dependencies too.
If you want a base class to make use of derived class implementations use the Template method pattern i.e add a virtual or abstract method in your base class and override and implement it in the derived class. You can then safely call this from the base class.
You can't do that because C does not necessarily implement the behaviours you created when you extended it in B.
So, say C has a method foo(). Then you know that you can call foo() on a B, as B extends C, so you can cast accordingly a treat a B as if it was a C with (C)(new B()).
However - if B has a method bar(), nothing in the subclass relationship says that you can call bar() on C too. Thus you cannot treat a C as if it were a B, and so you cannot cast.
In your exemple, you can cast n into a DerivedNode if you are certain that n is an instance of DerivedNode, or you can use generics:
public class Base<N extends BaseNode> {
protected N n;
public void foo(BaseNode x){
n.foo(x);
}
}
public class BaseNode {
public void foo(BaseNode x){...}
}
public class Derived extends Base<DerivedNode> {
public void bar(DerivedNode x){
n.bar(x); // no problem here - n DOES have bar
}
}
public class DerivedNode extends BaseNode {
public void bar(BaseNode){
...
}
}
Because if B extends C, then B might have stuff that isn't in C (like instance variables you initialize in the constructor that are not in new C())

Categories

Resources