How does inheritance work in this bit of code? - java

So guys I've been playing around with inheritance and I've stumbled upon this program :
public class HelloWorld {
static class A {
void f() { System.out.println("A"); }
}
static class B extends A {
void f() { System.out.println("B"); }
}
static class C {
void func(B b) { b.f(); }
}
static class D extends C {
void func(A a){ a.f(); }
}
public static void main(String args[]) {
( (new D())).func( (A) (new B()));
A a = new B();
a.f();
B b = new B();
C c = new D();
c.func(b);
}
}
So how come even though A and C are implemented exactly the same way in the final few lines, A's methods
get overriden by B, but C's don't get overriden by D?
The program prints as follows :
B
B
B

Because Class D function definition is more general than C. C's function takes B type parameter but D function takes type A parameter which is a parent of B. It is more general than a function defined in C.
static class D extends C {
void func(A a){
a.f();
}
}
B b = new B();
C c = new D();
c.func(b);
Variable c is pointing to D's object so c.func(b) invokes method defined in D. A is a parent of B hence B's method is called. Same as it is called using A's reference as shown below.
A a = new B();
a.f();

It is because the method func in D does not override the same of C as the signature change.
static class C {
void func(B b) { b.f(); }
}
static class D extends C {
void func(B a){ a.f(); }
}
This will result in an override of the method

Related

How come the Up-casting for c is redundant?

I am trying to figure out why the casting is redundant(thats the warning I get) in the end whats printed is "C".
public class Main {
public static void main(String[] args){
C c = new C();
B b1 = (B) c;
b1.f();
}
}
class A{
void f(){
System.out.println("A");
}
}
class B extends A{
}
class C extends B{
void f(){
System.out.println("C");
}
}
C extends B means that C is a B. And you do not need to cast C to B, because it IS a B.
So upcast (C to B) is always redundant. The only case when you would need cast is downcast (B to C):
B b = new C();
C c = (C) b;
Class c extends class B. So any C object is also a B object because of inheritance. So you do not need to explicitly cast '(B)' before c.

Java Inheritance and Method Dispatch

Given this Java code:
class A {
public void foo (Object o) { System.out.println("A"); }
}
class B {
public void foo (String o) { System.out.println("B"); }
}
class C extends A {
public void foo (String s) { System.out.println("C"); }
}
class D extends B {
public void foo (Object o) { System.out.println("D"); }
}
class Main {
public static void main(String[] args) {
A a = new C(); a.foo("Java");
C c = new C(); c.foo("Java");
B b = new D(); b.foo("Java");
D d = new D(); d.foo("Java");
}
}
why is the result ACBB?
I wil try to explain what I think, and I would appreciate if someone lets me know where my gap is.
So what I thought with the first two calls is:
a has static type A, but dynamic type C so Java should dispatch the method call dynamically and call foo() in C printing "C".
c has static and dynamic type C, so now since we inherit from A, it has to choose the most specific method, which is public void foo(String s) and thus printing "C"
b has static type B but dynamic type D so also in this case it should dynamically dispatch and call foo() in D printing "D".
d has static and dynamic type D, so now since we inherit from B, it has to choose the most specific method, which is public void foo(String o) and thus printing "B"
What is wrong in this explanation I've given here?
foo(Object) doesn't override foo (String) but overloads it. Hence D has 2 methods and since the most specific one will be used it will be foo(String) when you pass a string parameter.
From JLS 15.12.2 (emphasis by me):
This step uses the name of the method and the argument expressions to locate methods that are both accessible and applicable, that is, declarations that can be correctly invoked on the given arguments.
There may be more than one such method, in which case the most specific one is chosen.
A a = new C(); a.foo("Java"); calls foo(String) method from a which is a A
C c = new C(); c.foo("Java"); calls foo(String) method from c which is a C
B b = new D(); b.foo("Java"); calls foo(String) method from b which is a B
D d = new D(); d.foo("Java"); calls foo(String) method from d which only exists on B (D contains foo(Object))

Down Casting to indirect subclass does not work?

public class A
{
public void printA(){
System.out.println("A");
}
}
public class B extends A
{
public void printB(){
System.out.println("B");
}
}
public class C extends B
{
public void printC(){
System.out.println("C");
}
}
public class test {
public static void main(String[] args)
{
A a = new B();
a.printA(); // work
B b = (B) a;
b.printB(); // work
C c = (C) b;
c.printC(); // not work throw java.lang.ClassCastException
}
}
i have three classes A and B and C
C extends from B and B extends from A
why down casting work from A to B, and does not work from B to C ,although the relation between them like A and B , B is parent of C so how it work JVM??
Classes can only be cast to their parent classes, they have no knowledge about their subclasses.
Since your object is an instance of B, it does not implement methods of C.
This will work:
A a = new C();
a.printA(); // work
B b = (B) a;
b.printB(); // work
C c = (C) b;
c.printC(); // work

Why can I not call methods added to an anonymous class in Java?

If an anonymous class is extending/implementing a class/interface, why can't I add a new method?
In other words, this works:
class A {
void a() {
System.out.println("a in A");
}
}
class B extends A {
#Override
void a() {
System.out.println("a in B");
}
void b() {
System.out.println("b in B");
}
}
Why doesn't this work:
class C {
A anonA() {
return new A() {
void b() {
System.out.println("b in C");
}
};
}
}
Given:
public static void main(String[] args) {
B b = new B();
b.b();
// C c = new C();
// A anonA = c.anonA();
// anonA.b();
// yields: java: cannot find symbol \ symbol: method b()
}
Method invocations, at compile time, are determined based on the type of the expression they are invoked on. In your example, you are attempting to invoke b() on an expression of type A. A does not declare a b() method and so it won't work.
It does not work on your concrete B class example either
A notAnonA = new B();
notAnonA.b(); // fails to compile
You can very well add a new method inside the body of the new anonymous class, but you'll only be able to use it within the class or on the actual new anonymous class instance creation expression.
new A() {
void undeclared() {}
}.undeclared();

Overriding method with composition

Consider following situation. I want to achieve the different behavior for methoddA() of class A depending upon from where it is getting call like here from class D or class C. How this can be achieved, method overriding is not working here.
class A
{
public methodA(){ //some code }
}
class B
{
A a = new A()
public methodB()
{
a.methodA();
}
}
class C
{
B b = new B();
public methodC()
{
b.methodB();
}
}
class D
{
B b = new B();
public methodD()
{
b.methodB();
}
}
What you need here is Polymorphism. First create an interface -
public interface MyInterface
{
void methodA();
}
then create two different implementations for two different behaviors -
public class First implements MyInterface
{
public void methodA() {
// first behavior
}
}
public class Second implements MyInterface
{
public void methodA() {
// second behavior
}
}
Now create your other classes as follows -
class B
{
public void methodB(MyInterface m)
{
m.methodA();
}
}
class C
{
B b = new B();
public void methodC()
{
// Pass the corresponding behavior implementation
// as argument here.
b.methodB(new First());
}
}
class D
{
B b = new B();
public void methodD()
{
// Pass the second behavior implementation.
b.methodB(new Second());
}
}
This will result in a more maintainable code.
You can pass the class name to your method as a String and in your method check
if(className.equals("A") // or use isInstanceOf() if you are passing objects of A/B
//do something
if(className.equals("B")
// do something else.
Why do you need two different implementations?
This easy trick can work for you... Please correct me if i am wrong..
I following code I have modified the method signature of Class A1 and Class B1 to accept Object and similarly while calling the methods from Class C and Class D whereever we are calling this method of class B1 pass this as reference. In Class A1 we can then check instanceof object and identify the calling class.
class A1
{
public void methodA(Object c){ //some code }
if (D.class.isInstance(c)){
System.out.println("Called from Class D");
}else if (C.class.isInstance(c)){
System.out.println("Called from Class c");
}else{
System.out.println("Called from Some diff class");
}
}
}
class B1
{
A1 a = new A1();
public void methodB(Object c)
{
a.methodA(c);
}
}
class C
{
B1 b = new B1();
public void methodC()
{
b.methodB(this);
}
}
class D
{
B1 b = new B1();
public void methodD()
{
b.methodB(this);
}
}
public class Testnew{
public static void main(String args[]){
D d = new D();
d.methodD();
C c = new C();
c.methodC();
B1 b = new B1();
b.methodB(b);
}
}

Categories