public class Animal {
private String name;
public Animal(){
// empty constructor
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
An animal may or may not have a name, we can assign and retireve the name by its setter and getter methods.
Now, let's suppose we are interested in dogs. A dog may, or may not, have a name, but for shure a dog is of a specific breed (or not-so-specific crossbreed..). For the class Dog this means it shares the features of Animal plus some new features (its breed) - it extends the features of Animal. So Dog inherits from Animal, or in real-world language a Dog is an Animal. The class Dog will then look like the following
public class Dog extends Animal{
private String breed;
public Dog(String breed){
this.breed = breed;
}
public String getBreed(){
return breed;
}
}
The keyword extends indicates the 'is a'-relationship between Animal and Dog. Dog has all the attributes and methods of Animal plus its own attributes and methods. We can verify this
public static void main(String[] args) {
Dog dog1 = new Dog("Griffon");
dog1.setName("Barky");
System.out.println(dog1.getName()+" is a "+dog1.getBreed());
}
and the we get
Barky is a Griffon
So, the sublass (Dog) gets access to the features of the Superclass (Animal).
Note: A Subclasses can not directly access the private attributes of its Superclass, this will throw a java.lang.RuntimeException. To grant acces to the attributes of the superclass you can:
- make the attributes public (not so good..)
- make the attributes private and provide public or protected setters and getters
- make the attributes protected (visible in the whole package)
Let's take this a step further, say we have a vicious dog and there is a fair chance that it will bite the postman. To implement the class ViciousDog we can inherit from Dog (ViciousDog is a Dog) and extend it with a variable that holds the chance that the dog will bite.
Note: The following code is wrong and will not compile! I just want do demonstrate something.
public class ViciousDog extends Dog{
private int bites;
public ViciousDog(int bites){
this.bites = bites;
}
public int getBites(){
return bites;
}
public void setBites(int bites){
this.bites = bites;
}
}
try it out
public static void main(String[] args) {
ViciousDog vdog = new ViciousDog(50);
}
and we get a java.lang.RuntimeException.
End of wrong code.
Why's that?
When you instantiate a subclass it makes a call to the constructor of its superclass. All classes have an (empty) default constructor if not specified otherwise, so we could have omited the (empty) constructor in Animal. The call to the constructor of Animal in Dog happens automaically without the need of any additional code.
The constructor of Dog, however, awaits a parameter, the dogs breed. How do we call that constructor from ViciousDog? The solution is provided by the keyword super, which delegates parameters to the constructor of the superclass. The correct code for ViciousDog is then
public class ViciousDog extends Dog{
private int bites;
public ViciousDog(String breed, int bites){
super(breed);
this.bites = bites;
}
public int getBites(){
return bites;
}
public void setBites(int bites){
this.bites = bites;
}
}
Try it out again
public static void main(String[] args) {
ViciousDog vdog = new ViciousDog("Griffon",50);
vdog.setName("Barky");
System.out.println(vdog.getName()+" is a "+vdog.getBreed()+" and bites with a chance of "
+vdog.getBites()+" percent.");
}
the output is
Barky is a Griffon and bites with a chance of 50 percent.
We see that ViciousDog gets acces to the attributes and methods of Dog and the attributes and methods that Dog inheritited from Animal. Inheritance offers us a geat way of refactoring code, when designing a new class and you already have a class that has some of the desired features of you new class, you can simply inherit from that class and add all the extra functionality you need, without the need to design everything from scratch.
I hope this is useful to some of you new to Java or OOP.
Have fun, experiment and share your findings!
This post has been edited by rumzeis: 15 December 2010 - 12:52 PM







MultiQuote











|