• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call abstract class  RSS feed

 
Ramesh Sankar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
this is my coding .i have one abstract class that is exrended by class b..
i am creating obj reference for class a...
how will u call the abstract class method amethod() from class a..
the output should be 'b method 1';;
any one know?
Thanks
Ramesh.N


abstract class b{
private int i=1;
public void amethod(){
System.out.println("b method"+i);
}
}

public class a extends b{
private int i=2;
public void amethod(){
System.out.println("a method"+i);
}
public static void main(String arg[]){
b bobj=new a();
bobj.amethod();
}
}
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
General intent of abstract class is to provide implementation for method which requires no overriding.
So what is the intent of overriding abstract method which is implemented?
If you still desperate to call the abstract class method, then remove or change the method name in class b. Then you will be able to call abstract class method.
[ April 07, 2006: Message edited by: Arun Boraiah ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have one abstract class that is exrended by class b..

When I look at your code, it's the other way around - you have a class "a" that extends class "b". You don't have a class that is "extended by class b".

how will u call the abstract class method amethod() from class a..
the output should be 'b method 1';;


Add the following line to the implementation of amethod() in class a:

super.amethod();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!