1

Interface Price:

interface Price     {
    void printPrice();     }

AbstractTest class:

abstract class AbstractTest implements Price     {
        private String name, surname;

        AbstractTest (String name, String surname){
            this.name=name;
            this.surname=surname;     }

    public String getName()     {
        return this.name;     }

    public String getSurname()     {
        return this.surname;     }     }

TestAbstractTest class:

class TestAbstractTest extends AbstractTest{
    int price;

    TestAbstractTest(int price, String name, String surname){
        super(name, surname);
        this.price=price;     }

    void printPrice()     {
        System.out.println("price:"+ price)// HOW CAN I ACCESS TO NAME AND SURNAME FROM AbstractTest CLASS     }

Main class:

  class Main { 
    public static void main(String args[]) { 
       AbstractTest[] abstracttest= new AbstractTest[2];
       abstracttest[0]=new TestAbstractTest(5, xxx, yyy);
       abstracttest[1]=new TestAbstractTest(10, aaa, zzz);     }

I want to access the name and the surname from subclasses method printPrice.

I cant create an abstract method. I have to use printPrice from the interface Price. I've rearranged the constructor of TestAbstractTest like this (price, name, surname, AbstractTest abstracttest) and i've tried this.

abstracttest[0]= new AbstractTest(5, "xxx", "00yy", abstracttest[0])  

But its obviously wrong. How can I access name and surname and print it from printPrice method?

0

1 Answer 1

1

When declaring or requesting a variable's value you can use get() or set() methods.

abstracttest[0]= new AbstractTest(5, this.getName(), this.getSurname(), abstracttest[0]);

 System.out.println("price:"+ this.getPrice() );

You need to implement getPrice() of course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.