How to call a subclass method inside an another class rather than its super class?

Viewed 1867

I want to call a subclass function in another class in java.How do I do this?

AS an example here is my super class and subclass.

public abstract class a
{
  method 0;
}

class b extends a
{
   method 1;
}

There is another class call c. I wanna do the following operation in class c. C is an another class in a new file.But in the same package.

class c
{
   c val;
   public c
   {
      a var1 =( (b)val.method0()).method1;

   }
}

but I got an exception when running the program stating that cant cast type a to type b. Does anyone have a suggestion to get rid of this?

Here is the actual exception error I got.(I wrote above code as a demonstration)

Exception in thread "main" java.lang.ClassCastException: classfileparser.ConstantClass cannot be cast to classfileparser.ConstantUtf8 at classfileparser.ClassFile.(ClassFile.java:50) at classfileparser.ClassFileParser.main(ClassFileParser.java:18) C:\Users\Dave\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

4 Answers
Related