abstract

S

sujee

abstract class A{
//implemented methods
void test1(){System.out.println("test1: A");}

//abstract methods
abstract void test2();
}

class B extends A{

void test1(){System.out.println("test1: B");}
void test2(){System.out.println("test2: B");}

public static void main(String args[]){
B ins =new B();
ins.test1();
ins.test2();
}
}
I want to access the method test1() in the abstract class?? how can i?
 
D

David Van de Voorde

If inside class B you don't do anything with method test1(), then class B
will *inherit* the implementation of class A.
Since class A is abstract, you cannot handle an instance of class B *as if*
it was an instance of class A (= polymorphism).
So, you cannot: ((A)ins).test1();


David.
 
J

Joona I Palaste

David Van de Voorde said:
If inside class B you don't do anything with method test1(), then class B
will *inherit* the implementation of class A.
Since class A is abstract, you cannot handle an instance of class B *as if*
it was an instance of class A (= polymorphism).
So, you cannot: ((A)ins).test1();

Sorry, but this is pure nonsense. First of all, casting the reference
does not affect binding of the method name to the implementation.
Second, it's entirely possible to cast object references into abstract
class types. ((A)ins).test1() is entirely legal, but does the exact
same thing as ins.test1(). That's the whole *point* of things like
inheritance and abstract classes, you know.
A working solution was given by an earlier poster. You need to call
super.test1() from inside the method test1() in B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top