D
D
Dastan Namazov2015-03-24 09:27:41
Java
Dastan Namazov, 2015-03-24 09:27:41

Inheritance error?

class Man{  //суперкласс
    protected String name;
    protected int age;
    
    Man(String name, int age){
        this.name=name;
        this.age=age;
    }
    String getName(){
        return name;
    }
    int getAge(){
        return age;
    }
    public void tellAboutYourself(){
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

class Worker extends Man{  //подкласс
    protected int number;
    protected String email;
        
    Worker(String name, int age, int number, String email){
        super(name, age);
        this.number=number;
        this.email=email;
    }
    void work(){
        System.out.println("I'm working.");
    }
    public void tellAboutYourself(){
        System.out.println("Number: " + number);
        System.out.println("email: " + email);
        System.out.println("name: " + name);
        System.out.println("age: " + age);
    }
}

//использует методы классов Man и Worker
class Program{
    public void objects(){
        Man men = new Man();      //ошибка
        Worker worker = new Worker();
        men.tellAboutYourself();
        worker.tellAboutYourself();
        worker.work();
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
igorcc, 2015-03-24
@kb1rpb13

The problem is in the constructor call (there is no such constructor):
Man men = new Man(); //error
Create a default constructor

G
GavriKos, 2015-03-24
@GavriKos

1) Give the text of the error.
2) Where is the inheritance?

V
Viktor Koltcov, 2015-03-24
@Vityarik

Try this
Man men = new Man('Вася', 13); //ошибка

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question