Back to all lessons


Lesson 13

Inheritance

Task 0

Take a look at two classes below.


class A {
    private int x;
    private int y;
}
class B {
    private int x;
}
            

As you can see, A and B have field "x". We can say: A is the same like B, but with field "y".


class A extends B {
    private int y;
}
class B {
    private int x;
}
        

In such way we can code less and have same results. It also helps us managing huge projects.

Does it work only with properties?

Task 1

Create a class Person, which contains:

  1. String name
  2. int yearOfBirth
  3. int getAge()

Then create a class Student and Professor. Each of them should extend class Person and also contains additonal fields.

Try to create objects for each of those 3 classes.

Task 2

Comment those statements, which are not valid. Explain: why?


Student s = new Professor();
Professor p = new Student();
Person sPerson = new Student();
Person pPerson = new Professor();
Student personA = new Person();
Professor personB = new Person();
                
Task 3

Create a new class with 2 fields:

  • protected a
  • private b

Then create another class, that will extend the first one.

Create constructor for each class and try to assign value for each field.

Comment those lines, which are not correct and explain: what is the difference between protected and private?

Task 4

What will be printed, if we execute the snippet below:


Object anObject = new Object();
System.out.println(anObject);
System.out.println(anObject.toString());
            

What is the difference between using and not using toString()?

Task 5

Each class extends class Object, which contains function String toString().

Choose any class created during lesson and write own toString

Task 6

Create a new package: bakery. Create new classes: Person, Baker, Roll, Bread, SeedFood, Food, Bakery, City. Create all inheritance relations.

Task 7

Create a method to allow Person to eat.

Task 8

Allow Bakery to be placed in City (one Bakery is enough). Add array of Persons (different kind) into City.

Task 9

Create new object of Baker and put him to an instance of Bakery. After calling method "preheatStove", bakery can start working. Otherwise, it's not working. When bakery works, it produces Bread(s) and Roll(s) and stores inside of one array.

Task 10

Every person can buy a Roll or Bread (to simplify let's say, the person is not selecting what to buy).

Create a bakery and fill array of persons in a City constructor.

Task 11

Create a method called: nextDay(). It starts Bakery, that produces food (only if in City lives a Baker).

nextDay method also allow everybody to eat food from Bakery (choose any strategy to split food between Persons). If somebody doesn't eat, he will die.

If some amount of Persons will eat, the new Person is born in the City (add to array).

Task 12

Run a simulation of city. Iterate nextDay() until: there are people or 1000 days passed.

Set parameters of your program and see how the city works.

Print results as rows like: [number of day] [amount of people]