Inheritance.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write them yourself

To be able to understand this we are going to see an example. In this example we are going to create an hotel for dogs where we are going to register them by name, weight and colour, reusing the class Dog from the past tutorials.

 package com.edu4java.tutorial13;

/**
 * A class is a mould from which we can create objects or instances
 * An object is an instance of a class
 * 
 * JavaBeans are reusable software components for Java
 * that can be manipulated visually in a builder tool.
 */
public class Dog {
	// instance variables

	private String name;
	private double weight;
	private String colour;

	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}
	

	public double getWeight() {
		return weight;
	}


	public void setWeight(double weight) {
		this.weight = weight;
	}


	public String getColour() {
		return colour;
	}


	public void setColour(String colour) {
		this.colour = colour;
	}


	public void printToConsole() {
		System.out.print(" name: " + getName());
		System.out.print(" colour: " + this.colour);
		System.out.println(" weight: " + this.weight);

	}
}

 

After registering the dogs with the method insertDog() we print them on Console to see each one of the dogs. In the next instruction of class "Tutorial13", we feed the dogs with 500gr of food with the method feed(dogs) and we print them again to see that they now weigh 500 grams more.

 package com.edu4java.tutorial13;

public class Tutorial13 {

	public static void main(String[] args) {
		Dog[] dogs = insertDog();
		printDogsOnConsole(dogs);
		feed(dogs);
		System.out.println("After eating------------");
		printDogsOnConsole(dogs);

	}

	private static void feed(Dog[] dogs) {
		for (int i = 0; i < dogs.length; i++) {
			// we give feed them with 500gr of food
			double weightBeforefeeding = dogs[i].getWeight();
			dogs[i].setWeight(weightBeforefeeding + 0.5);
		}

	}

	private static void printDogsOnConsole(Dog[] dogs) {
		for (int i = 0; i < dogs.length; i++) {
			dogs[i].printToConsole();
		}

	}

	private static Dog[] insertDog() {

		Dog[] dogs = new Dog[4];

		// list of Dogs
		String[] names = { "Coco", "Sultan", "Boby", "Drak" };
		String[] colours = { "brown", "black", "white", "blue" };
		double[] weight = { 1.5, 75, 3.5, 45.1 };

		for (int i = 0; i < dogs.length; i++) {
			Dog dog = new Dog();
			dog.setName(names[i]);
			dog.setColour(colours[i]);
			dog.setWeight(weight[i]);
			dogs[i] = dog;

		}
		return dogs;
	}

}

 

We now want to add a new property to our dog; "portion". We can´t modify the class "Dog" because it is also been used in other classes, so we create a new class Dog2, with our new property "portion", and we extend it from Dog. In this way we have a Dog2 with all the properties of our old Dog; "name", "colour" and "weight" and also the new property "portion", added in class Dog2.

 package com.edu4java.tutorial13;

public class Dog2 extends Dog {

	private double portion;

	public double getPortion() {
		return portion;
	}

	public void setPortion(double portion) {
		this.portion = portion;
	}
	
	
}

We now work with "Dog2" class replacing it where we had "Dog" and we use it to give a specific portion to each of the dogs, depending on their weight;

 package com.edu4java.tutorial13;

public class Tutorial13 {

	public static void main(String[] args) {
		Dog2[] dogs = insertDog();
		printDogsOnConsole(dogs);
		feed(dogs);
		System.out.println("After eating------------");
		printDogsOnConsole(dogs);

	}

	private static void feed(Dog2[] dogs) {
		for (int i = 0; i < dogs.length; i++) {
			double weightBeforefeeding = dogs[i].getWeight();
			dogs[i].setWeight(weightBeforefeeding + dogs[i].getPortion());
		}

	}

	private static void printDogsOnConsole(Dog2[] dogs) {
		for (int i = 0; i < dogs.length; i++) {
			dogs[i].printToConsole();
		}

	}

	private static Dog2[] insertDog() {

		Dog2[] dogs = new Dog2[4];

		// list of Dogs
		String[] names = { "Coco", "Sultan", "Boby", "Drak" };
		String[] colours = { "brown", "black", "white", "blue" };
		double[] weight = { 1.5, 75, 3.5, 45.1 };
		double[] portion = { 0.2, 1, 0.2, 0.8 };

		for (int i = 0; i < dogs.length; i++) {
			Dog2 dog = new Dog2();
			dog.setName(names[i]);
			dog.setColour(colours[i]);
			dog.setWeight(weight[i]);
			dog.setPortion(portion[i]);
			dogs[i] = dog;

		}
		return dogs;
	}

}

 
<< Previous Next >>