0

I am learning java from last month. I am having a doubt regarding abstract classes. I am trying to add another method which in abstract class test, but complirer is throwing an error. Could you please tell what i had done wrong in this code ?

Error - cannot find symbol - method err()

 class abstct 
{
    public static void main(String args[])
    {
        one obj = new error();
        obj.show();
        obj.test();
        obj.err();
    }
}
abstract class one
{
    abstract void show();
    abstract void test();
}
abstract class two extends one
{
    public void show()
    {
         System.out.println("Understood");
    }
    abstract void test();
}
abstract class three extends two
{
    public void test()
    {
        System.out.println("Understood");
    }
    abstract void err();
}
class error extends three
{
    public void err()
    {
       System.out.println("Here I am getting error .. ??");
    } 
} 
Pradeep
  • 19
  • 2

1 Answers1

3

Please follow java standard and set uppercase the first letter of the class.

Here you instanciate an Error class but you put it in a Inter properties. it is valid as Error extends Inter. But it also mean that you can only use the method of the Inter properties. So you have access to show and test.

Here is the code you can use

public class Abstr {
    public static void main(String args[]) {
        Inter obj = new Error();
        if (obj instanceof Test){
            ((Test)obj).err();
        }
        obj.show();
        obj.test();
    }
}

abstract class Inter {
    abstract void show();

    abstract void test();
}

abstract class Impl extends Inter {
    public void show() {
        System.out.println("Understood");
    }
}

abstract class Test extends Impl {
    public void test() {
        System.out.println("Understood");
    }

    abstract void err();
}

class Error extends Test {
    public void err() {
        System.out.println("Here I am getting error .. ??");
    }
}

note in jdk14, there is a preview of pattern matching, and if enable you could write:

if (obj instanceof Test){
   obj.err();
}

wargre
  • 4,575
  • 1
  • 19
  • 35