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.


  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");
        Animal dog = new Animal("Dog", "Maks", "Black");

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

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

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

Everything was clear?

Section 5. Chapter 4