Calling Method That Exists in Child Classes But Not in Parent Class

Calling method that exists in child classes but not in parent class

The approach that I am finally taking is to define an interface say FooInterface with foo() method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo() method like this:

Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface){
((FooInterface)obj).foo();
}

Calling method from java child class not in parent class

  1. Java is statically typed language, so a variable type should be known at compile time. If type that is known to the compiler doesn't expose such a method - it is compilation failure. Exactly for the reason to not let it to runtime.

  2. Why should it? It just finds the method body late, not signature. Still, static typing means that signature must be satisfied at compile time.

  3. At runtime JVM tries to find the most specific implementation. In SportsCar, in your case. If it is inherited from parent but absent in child - parent's code is used.

If you need to call method from specific child which is absent in variable type - you can cast it at runtime at your own risk of getting ClassCastException.

Child Method not recognised from Parent class TypeScript

i try to use a child method from the parent class but the method is not recognised

...

as i said i'm new to oop, it's not a good practice to do this kind of call ?

Right. Parent classes shouldn't call methods defined by child classes. One reason for that is that the instance where you're making the call may not be an instance of the child class (it could be just an instance of the parent, or the instance of another child). Consider this simpler example:

class Parent {
parentMethod() {
this.childMethod(); // Won't compile in TypeScript
}
}
class Child extends Parent {
childMethod() {
// ...
}
}
const p = new Parent();
p.parentMethod(); // Would throw an error in JavaScript, because `parentMethod`
// tries to use `childMethod`, which doesn't exist -- it's a
// `Parent`, not a `Child`

And a similar issue exists if there's Child2 and Child2 doesn't have the method, and you have

const c2 = new Child2();
c2.parentMethod(); // Would throw in JavaScript

Those "would throw in JavaScript" are one of the reasons TypeScript is useful — it warns you of these things at compile-time instead of runtime.

If the parent should be making calls that the child may want to handle differently, you define the method on the parent, and override it on the child. For instance:

class Parent {
parentMethod() {
this.theMethod();
}
theMethod() {
// ... this might even not do anything ...
}
}
class Child extends Parent {
theMethod() {
// ... this can do something slightly different from `Parent`'s version
// Can access `Parent`'s version if desired via `super.theMethod()`.
}
}
const p = new Parent();
p.parentMethod(); //Works

You might want an abstract method: That would mean you could never create instances of Parent, and subclasses would have to implement the method:

class Parent {
parentMethod() {
this.theMethod();
}
abstract theMethod();
}
class Child extends Parent {
theMethod() {
// ... this can do something slightly different from `Parent`'s version
// Can access `Parent`'s version if desired via `super.theMethod()`.
}
}
const c = new Child();
c.theMethod(); //Works
const p = new Parent(); // Won't compile, because `Parent` is abstract

Why parent class method is getting called and not child class in this case?

When you are using a reference of type A, you see only the methods defined for class A. Since doSomething in B doesn't override doSomething in A (since it has a different signature), it is not called.

If you were to use a reference of type B, both methods would be available, and doSomething of B would be chosen, since it has a more specific argument (String vs Object).

When child class is calling parent class method, why it is printing parent class variable?

When you call a method on an instance of a class, Java will first try and find the method in the subclass, and if not found, it will try and look in the superclass.

In your code, when you call r.showName(), it will first check for that method in the Rabbit class, and since it does not find it there, it checks the superclass Animal. It finds it in Animal, so it runs the method it finds there.

If you want r.showName() to say rabbit, you have to override the method in the Rabbit class.

Setter in parent class, with some child classes that don't have the parameter

class A has overriden the method setParam of class Parent. Even though you assign A instance as Parent class, call on a.setParam() still behave as subclass A. That's what called "subclass polymorphism".

class B overrides nothing of Parent. So b.setParam() just the call b's parent's setParam method, and nothing happen.



Related Topics



Leave a reply



Submit