Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Child class method from Parent class method

Consider the following case

class A
{
    X()
    {
        //some code
        Y();
    }
}

class B extends A
{
    Y(){ //some code }
}

If I create an object of the class B, and i try to use function X as it is extended from A, is it possible that the function X can access the function Y of the calling object of class B.

like image 345
Kaushik Vijayakumar Avatar asked Jun 21 '26 04:06

Kaushik Vijayakumar


1 Answers

you could but this must be met

  1. A class is abstract
  2. Y method is abstract too

Example:

abstract class A {
    void X() {
        Y();
    }

    abstract void Y();
}

class B extends A {
    @Override
    void Y() {
        System.out.println("Hello from B class");
    }
}

in that form every time class A calls Y, it will actually invoke an implememted method of the abstract method Y

like image 75
ΦXocę 웃 Пepeúpa ツ Avatar answered Jun 23 '26 18:06

ΦXocę 웃 Пepeúpa ツ