Once upon a time, in the mystical land of Java, there existed a wise and powerful creature known as Master Oogway. He was no ordinary being but an abstract class, revered as the commander and the master of all warriors, including Master Shifu. Each master possessed secret skills, represented by private skills and weapon, which were known only to them. However, there were certain skills that they were willing to share with those who aspired to follow in their footsteps. These special skills were protected and accessible only to their students.

Master Oogway, being the epitome of tranquillity, held the private skill of attaining inner calm. In addition, he entrusted Master Shifu with a protected skill, teaching him the art of saving the temple from any threat. Under Master Oogway’s guidance, Master Shifu honed his abilities and trained diligently, preparing to face any challenges that came his way.

Each master possessed secret skills that were passed down through generations.

Explaining Java Inheritance using Kungfu Panda story covering abstract, sealed, final classes

Master Oogway, with his deep understanding of the universe, had mastered the art of harnessing the power of chi. This secret skill allowed him to achieve a state of calmness and centeredness, even in the face of adversity. He kept this skill private, sharing it only with his most trusted students.

On the other hand, Master Shifu, a disciplined and dedicated student, had developed his own secret skill. He had mastered the art of using pressure points in combat, enabling him to defeat opponents with precision and efficiency. This skill was protected and passed down only to his most promising students.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
abstract class Oogway {
    private String Calmness; //skill
    private String SoloThought; //skill
    protected String PhoenixBlade;

	abstract public void fight();
}

class Shifu extends Oogway {
	private List<String> pressurePoints;

	public void fight(){
		System.out.println("Defeat with: " + pressurePoints);
	}
	public void fight(String level){
		if(level.equals("final")) System.out.println("Using protected weapon: " + PhoenixBlade);
	}
}

public class Main {
    public static void main(String[] args) {
        Shifu shifu = new Shifu();
        shifu.fight();
        shifu.fight("final");
    }
}

In their world, the masters possessed a unique magic that allowed them to create virtual images, or instances, of themselves. These images were sent into battle to protect the village and its inhabitants. Any villager in need could request the masters to create an image and accompany them in their quest to safeguard the village. However, Master Shifu possessed some static skills like the ability to open the gate of the temple effortlessly. He doesn’t need any of his images to do the same.

Explaining Java instances using Kungfu Panda story

To prevent malevolent warriors, such as the infamous Kai, from harnessing their powers, both Master Oogway and Master Shifu sealed their skills securely. They were well aware of the potential dangers that could arise if their knowledge fell into the wrong hands. Additionally, both masters were marked as Temple Savers, an honorable position signifying their commitment to protecting the sacred grounds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sealed class Oogway permits Shifu {
    // properties and methods of Oogway class
}

sealed class Shifu permits Po, Tigress {
    // properties and methods of Shifu class
}

class Po extends Shifu {
    // properties and methods of Po class
}

class Tigress extends Shifu {
    // properties and methods of Tigress class
}

Every Monday, villagers flocked to the temple seeking the guidance and training of the masters. Regardless of who was present, any master bearing the Village Trainer interface was capable of providing instruction. The training modules implemented polymorphism, not concerning themselves with the specific identity of the master. As long as they possessed the necessary qualifications, any master could impart their wisdom. Just like polymorphism, allowed different masters to train the villagers or other master who is recognized as a trainee.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
abstract sealed class Oogway permits Shifu {
    public void train() {
        System.out.println("Oogway is training");
    }
}

sealed class Shifu permits Po, Tigress implements Trainer{
    public void train() {
        System.out.println("Shifu is training");
    }
}

interface Trainer {
    default void train(Trainee trainee) {
        trainee.train();
    }
}

interface Trainee {}

class Po extends Shifu implements Trainer, Trainee {}

public class TrainingScheduler {
	List<Trainer> trainers;
	List<Trainee> trainees;
	Map<Trainer,Trainee> schedule; 

	public void schedule(){
		//for each trainee in trainees
		// pick a trainer from trainers randomly
		// assign a trainer to trainee and save the schedule
	}
}

One fateful day, a simple panda stumbled upon the temple. His eyes widened in awe as he spotted Master Shifu deep in meditation. The panda had long admired the master from afar and approached him with a humble request—to become a master like him. At first, Master Shifu hesitated, unsure if the panda had what it took to walk the path of a master. Yet, the panda’s unwavering determination and numerous attempts to convince him eventually persuaded Master Shifu to accept his request. He set one condition: the panda must become the final inheritor of his skills, unlike the non-sealed masters before him.

As the panda progressed in his training, he faced numerous challenges and obstacles. He had to prove his worthiness by going through intense combat sessions, mastering meditation techniques, and demonstrating his commitment to protecting the temple. Through his determination and hard work, the panda eventually became a master, inheriting the secret skills of Master Shifu.

Meanwhile, a powerful enemy threatened the village and the temple. The masters realized that they needed to train the villagers to defend themselves. The panda, now a master, took on the responsibility of training the villagers, using the skills he had learned from Master Shifu. The village united under the guidance of the masters and successfully defended their home.

In the end, the temple remained a symbol of strength and resilience. The panda, now a respected master, continued to train future generations, passing down the ancient knowledge and skills that had been entrusted to him.

This story showcases the importance of mentorship, dedication, and the power of perseverance. It highlights the concept of polymorphism, where different masters can train individuals as long as they possess the necessary skills and knowledge. It also emphasizes the significance of protecting valuable traditions and passing them down to future generations.”

As the panda continued his training under the guidance of Master Shifu, he learned about the concept of non-changeable skills and weapons, known as the final. His images can use them as it is. Even the maters of Panda type can’t change how those weapons work. With the permission of Master Shifu, Po finally removed the final from his type so he can make masters of his type.

1
2
3
4
5
6
7
class Po extends Shifu {
    final String secretSauce = "AX87DF89E3";

    final void momoBlast() {
        System.out.println("Fire the momo blast!");
    }
}

During one eventful day, Master Shifu presented a final green-stone to the panda. This unique stone possessed a special power to freeze the enemy power for sometime. Intrigued, the panda decided to create a “green-stone” of his own to experiment and develop a new weapon similar to the one Master Shifu possessed. However, an unexpected interruption occurred when some villagers approached the panda, requesting his assistance in fighting against their enemies. Unfortunately, the panda forgot to remove the weapon he had recently created and inadvertently shared his image with the villagers.

Explaining Java variable shadowing using Kungfu Panda story

Though villagers somehow won the fight, he got to know that “great-stone” didn’t work at all. He was surprised as Master Shifu himself showed his power to him. He went to Master Shifu to know why it failed in the war field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Shifu {
    //Uncomment any line you want, output will not change
    protected final String greenStone = "green-stone";
    // protected String greenStone = "green-stone";
    // final String greenStone = "green-stone";
}

class Po extends Shifu {
    String greenStone = "po-green-stone";
}

public class Main {
    public static void main(String[] args) {
        Po po = new Po();
        System.out.println(po.greenStone); // po-green-stone
    }
}

Master Shifu used a metaphor to explain this concept. He said, “Imagine that our final weapons are like two lanterns shining their light. When we have the same name for our weapons, it’s like placing one lantern directly in front of the other. The light from the first lantern will be hidden, and only the light from the second lantern will be visible.”. He immediately realised his mistake of not erasing the weapon he created just for the experiment before making sharing his image with the villagers. Master Shifu advised the panda to choose a different name for his weapon to prevent any conflicts. He named it “light-green-stone”. Both clutched their stomachs and erupted in laughter, seemingly oblivious to anyone observing them.

No matter what speciality your master had. If you have something that your master also had, people will see you first. Because you’re concrete.

The Palace managed by Master Shifu can be considered as a Java microservice. It is the only palace on Jade Mountain and functions independently without being dependent on other temples. The Palace serves as a self-contained unit, providing specific services to its visitors.

To manage the increasing workload of the Temple, a new library is created as a sub-unit or module within the Palace. The library operates as an independent entity, handling specific tasks and responsibilities. Despite being a part of the Temple, the library has its own separate functionality and can be accessed and utilized by villagers and another modules of the palace independently.

In the context of Java, modules are a way to organize and encapsulate code. They allow for the separation of concerns and provide a modular structure to the application. Each module can have its own set of functionalities and dependencies. It provides a clear boundary for code ownership and enables different teams to work on different modules independently. A module can be considered as an independent microservice that is tightly coupled with a service but can work independently. A module can also be created when a micro-service is utilize the same resources and technology stack and has some meaning to be the part of same project.

⚠️ Warning

In this article, inheritance is considered as a type relationship not as a “parent-child” relationship. Think in this way, Human class is inherited by Superman class. But Superman is not the child of the human but it is the type of Human.

In the next article, I’ll explain to you all the Java concepts covered in this article. So bookmark this article.

Have any thoughts to improve this article, correct the concepts and cover more concepts? Then share your thoughts.



Your feedback is really important. So I can improve my future articles and correct what I have already published. You can also suggest if you want me to write an article on particular topic. If you really like this blog, or article, please share with others. That's the only way I can buy time to write more articles.

Amit
Amit Author & Maintainer of many opensourse projects like fast-xml-parser, imglab, stubmatic etc. Very much interested in research & innovations.

Related Articles

Chapter 2: The Preparation of Battle
Amir Wishu Sehgal cafe: The story of opportunities
Story of Kungfu Java Valley - Explanation