Index
Overview


Class inheritance is easy to add in Java - we simply add the keyword "extends" as we'll show in examples.

Example #1


public abstract class Shape {
	//Ivars
	private Color color;
	private int lineThickness;
	//Instance Methods
	public void setRandomColor() {
		//...
	}
	public void setRandomLineThickness() {
		//...
	}
}

public class Rectangle extends Shape {
	//...
}

public class RightTriangle extends Shape {
	//...
}

public class Circle extends Shape {
	//...
}

Example #2


public abstract class Pet {
	//Ivars
	private String name;
	private String favoriteFood;
	//Instance Methods
	public void setName(String nm) {
		//...
	}
	public void setFavoriteFood(String food) {
		//...
	}
}

public class Dog extends Pet {
	//...
}

public class Cat extends Pet {
	//...
}

public class Hamster extends Pet {
	//...
}