ChallengeChallenge

Task

There is a code that is currently not functioning. The toString() method has already been overridden in the method, so you don't need to worry about it. Your task is to bypass the private access modifier and create two objects with the necessary parameters.

java

Main.java

  1. 1. There are two ways to bypass this access modifier. The first one is to initialize the objects through the constructor;
    1. 2. The second one is to remove the private access modifiers and replace them with public (but this is considered bad practice, so it's better to use the first method).

package com.example;

public class Main {
    public static void main(String[] args) {
        Animal cat = new Animal("Cat", "Garfield", "Red");
        System.out.println(cat);
        Animal dog = new Animal("Dog", "Maks", "Black");
        System.out.println(dog);
    }
}

class Animal {
    private String type;
    private String name;
    private String color;

    public Animal(String type, String name, String color) {
        this.type = type;
        this.name = name;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Animal{" + "type='" + type + '\'' + ", name='" + name + '\'' + ", color='" + color + '\'' + '}';
    }
}
      

Everything was clear?

Section 5. Chapter 4