PDA

View Full Version : Calling an Abstract Class' Method


webguy08
04-06-2009, 11:15 PM
Hi all,
I am writing a Java program, but have come across a problem.

I have an abstract class (named Customer) which contains a method: getCustomer(). This returns an object of type Object.
I then have a different (concrete) class which needs to call that method to retrieve the Object. It is not possible for me to create a new object of that class because it is abstract, so what should I do? :confused:

I tried the following line of code: Object customer = Customer.this.getCustomer(); but I get an error saying: "No enclosing instance of the class Customer is accessible in scope."

I have no idea what the error means, nor how I can overcome this problem; other than making the Customer class concrete :(

Thanks for any help!

servlet
04-07-2009, 06:43 AM
I have few questions..

First of all, I don't understand this line of code

Object customer = Customer.this.getCustomer()

1. What is 'this' here?
2. Do you have variable 'Customer' declared above this line?

Why getCustomer() method returns object of type Object? as per the name, it should return object of type Customer.

Does the concrete subclass overrides this method? is this method abstract in Customer class?

What you should have?
- getCustomer() method is declared abstract in Customer class. and it returns object of type Customer.
- Sublclasses overrides this method.

Then also i don't understand, why concrete subclasses needs to call this method.

It seems that there are problems in your design, first of all understand the responsibility of each classes and assign behavior accordingly. In OOP classes should do only those things they are responsible for.

webguy08
04-07-2009, 02:46 PM
I have few questions..

First of all, I don't understand this line of code

Object customer = Customer.this.getCustomer()

1. What is 'this' here?
2. Do you have variable 'Customer' declared above this line?

Why getCustomer() method returns object of type Object? as per the name, it should return object of type Customer.

Does the concrete subclass overrides this method? is this method abstract in Customer class?

What you should have?
- getCustomer() method is declared abstract in Customer class. and it returns object of type Customer.
- Sublclasses overrides this method.

Then also i don't understand, why concrete subclasses needs to call this method.

It seems that there are problems in your design, first of all understand the responsibility of each classes and assign behavior accordingly. In OOP classes should do only those things they are responsible for.

1. 'this', as far as I know, enables me to call methods from other classes. I haven't used it before which is why I am confused.
2. I haven't got a variable named 'Customer'. Customer is not a variable but a class.

getCustomer() returns an Object because the Customer class has subclasses which hold different types of customer, thus when passing the type of customer to other classes it is unknown what subclass (type of customer) it will pass.

The method getCustomer() is a concrete method in the Customer class.

The concrete class needs to call this method because it needs to get the data of the customer, which is held in the Customer class.

It's all quite confusing, but I need to be able to use methods from the Customer class whilst at the same time keeping it abstract. Is this possible?

webguy08
04-07-2009, 06:21 PM
Tell you what, delete this thread. My design is clearly at fault :)