-> what is inheritance in java

This page describe when you want to overwrite a particular class if it differs from the standard method.

Example:

To inherit the characteristics of a class we saw on the inheritance page that, we could use the keyword extends. Subsequently we make a super-class in which we could write a method which hold for all classes that uses that same method. So, if I’ve a dog and a cat that are both black I am able to categorise my 2 pets in one group: black pets. Because they have something in common, therefore my method which prints the color is for both the same.

In the case that I have 9 pets, which are all black, this is a good way to deal with this problem, because I only have to add once: that my pets are black. Plus, every order characteristic they got in common I could add to my blackPets class. So I want to add that all my black pets have green eyes as well.

package Inheritence;

public class cat extends blackPets {

	void color () {
		System.out.println("color = black and white");
	}
	
	void eyeColor() {
		System.out.println("eye color = green");
	}
}
package Inheritence;

public class pets {

	
	void start() {
		dog myDog = new dog();
		cat myCat = new cat();
		
		myDog.color();
		myCat.color();
		myDog.eyeColor();
		myCat.eyeColor();
	}
	
	public static void main(String[] args) {
		new pets().start();

	}

}

Now the problem. I’ve 9 pets all black, but one cat has a black fur with some white stripes. I want that all the characteristics for my black cats hold true, therefore I only want to change their color to black and white.

Problem: want to change one characteristic of one of my pets.

To solve this problem: I can simply write in the class of the black and white cat;

package Inheritence;

public class catOtherColor extends blackPets {
	
	void color () {
		System.out.println("color = black and white");
	}
	
}
package Inheritence;

public class pets {

	
	void start() {
		cat myCat1 = new cat();
		cat myCat2 = new cat();
		cat myCat3 = new cat();
		cat myCat4 = new cat();
		cat myCat5 = new cat();
		cat myCat6 = new cat();		
		cat myCat7 = new cat();	
		cat myCat8 = new cat();		
		catOtherColor myCat9 = new catOtherColor();
		

		myCat1.color();
		myCat2.color();
		myCat3.color();
		myCat6.color();
		myCat5.color();
		myCat6.color();	
		myCat7.color();
		myCat8.color();
		myCat9.color();
		
	}
	
	public static void main(String[] args) {
		new pets().start();

	}

}

Output:

color = black
color = black
color = black
color = black
color = black
color = black
color = black
color = black
color = black and white

Easy ;p