You may have noticed in the above example that the derived
class produced the same results as the base class. In reality, inheritance
is used to solve various object-oriented programming (OOP) problems. One of
them consists of customizing, adapting, or improving the behavior of a method of the parent class. For example,
although both the circle and the sphere have an area, their areas are not
the same.
A circle is a flat surface but a sphere is a volume, which makes
its area very much higher. Since they use different formulas for
their respective area, you should implement a new version of the area in
the sphere.
When deriving one class from another class,
you should be aware of the fields and methods of the base class so that,
if you know that the parent class has a certain behavior or a
characteristic that is not conform to the new derived class, you can do
something about that.
Providing a new version of a method that exists in the
parent is referred to as overriding it.
To override a method, start it with the expression @Override.
Then create the new version of the method. It must use the same return type, the
same name, and the same arguments, if any, of the parent class. Here is an
example:
class Circle { private double radius; public double getRadius() { return radius; } public void setRadius(double value) { if( radius < 0 ) radius = 0.00; else radius = value; } public double calculateDiameter() { return radius * 2; } public double calculateCircumference() { return calculateDiameter() * 3.14159; } public double calculateArea() { return radius * radius * 3.14159; } } class Sphere extends Circle { @Override public double calculateArea() { return 4 * getRadius() * getRadius() * 3.14159; } } public class Exercise { public static void main(String[] args) { Circle round = new Circle(); round.setRadius(25.55); System.out.println("Circle Characteristics"); System.out.printf("Side: %f\n", round.getRadius()); System.out.printf("Diameter: %f\n", round.calculateDiameter()); System.out.printf("Circumference: %f\n", round.calculateCircumference()); System.out.printf("Area: %f\n", round.calculateArea()); Sphere ball = new Sphere(); ball.setRadius(25.55); System.out.println("\nSphere Characteristics"); System.out.printf("Side: %f\n", ball.getRadius()); System.out.printf("Diameter: %f\n", ball.calculateDiameter()); System.out.printf("Circumference: %f\n", ball.calculateCircumference()); System.out.printf("Area: %f", ball.calculateArea()); } }
This would produce:
Circle Characteristics Side: 25.550000 Diameter: 51.100000 Circumference: 160.535249 Area: 2050.837806 Sphere Characteristics Side: 25.550000 Diameter: 51.100000 Circumference: 160.535249 Area: 8203.351224
Notice that, this time, the areas of both figures are
not the same even though their radii are similar.
Besides customizing member variables and methods of a
parent class, you can add new members as you wish. This is another
valuable feature of inheritance. In our example, while a circle is a flat
shape, a sphere has a volume. In this case, you may need to calculate the
volume of a sphere as a new method or property of the derived class. Here
is an example:
class Circle { private double radius; public double getRadius() { return radius; } public void setRadius(double value) { if( radius < 0 ) radius = 0.00; else radius = value; } public double calculateDiameter() { return radius * 2; } public double calculateCircumference() { return calculateDiameter() * 3.14159; } public double calculateArea() { return radius * radius * 3.14159; } } class Sphere extends Circle { @Override public double calculateArea() { return 4 * getRadius() * getRadius() * 3.14159; } public double calculateVolume() { return 4 * 3.14159 * getRadius() * getRadius() * getRadius() / 3; } } public class Exercise { public static void main(String[] args) { Circle round = new Circle(); round.setRadius(25.55); System.out.println("Circle Characteristics"); System.out.printf("Side: %f\n", round.getRadius()); System.out.printf("Diameter: %f\n", round.calculateDiameter()); System.out.printf("Circumference: %f\n", round.calculateCircumference()); System.out.printf("Area: %f\n", round.calculateArea()); Sphere ball = new Sphere(); ball.setRadius(25.55); System.out.println("\nSphere Characteristics"); System.out.printf("Side: %f\n", ball.getRadius()); System.out.printf("Diameter: %f\n", ball.calculateDiameter()); System.out.printf("Circumference: %f\n", ball.calculateCircumference()); System.out.printf("Area: %f\n", ball.calculateArea()); System.out.printf("Volume: %f", ball.calculateVolume()); } }
This would produce:
Circle Characteristics
Side: 25.550000
Diameter: 51.100000
Circumference: 160.535249
Area: 2050.837806
Sphere Characteristics
Side: 25.550000
Diameter: 51.100000
Circumference: 160.535249
Area: 8203.351224
Volume: 69865.207924
To maintain a privileged relationship with its
children, a parent class can make a member available only to
classes derived from it. With this relationship, some members of a parent class have a
protected access level. Of course, as the class creator, it is your job to
specify this relationship.
To create a member that
derived classes can access, type the protected keyword to its left.
If you declare of the class, you can call still access its public members. Here
are examples:
class Person { private String _name; private String _gdr; public Person() { this._name = "Not Available"; this._gdr = "Unknown"; } public Person(String name, String gender) { this._name = name; this._gdr = gender; } protected String getFullName() { return _name; } protected void setFullName(String value) { _name = value; } protected String getGender() { return _gdr; } protected void setGender(String value) { _gdr = value; } public void show() { System.out.printf("Full Name: %s\n", this.getFullName()); System.out.printf("Gender: %s", this.getGender()); } } public class Exercise { public static void main(String[] args) { Person man = new Person("Hermine Sandt", "Male"); System.out.println("Staff Member"); man.show(); } }
This would produce:
Staff Member Full Name: Hermine Sandt Gender: Male
If you create a class member and mark it as protected, the
other classes of the same application (package) and the classes derived from
that parent class can access those protected members. This means that, unlike
C++ and C#, classes not derived from that class but that belong to the same
program can access the protected members of the class. Here are examples:
class Person { private String _name; private String _gdr; public Person() { this._name = "Not Available"; this._gdr = "Unknown"; } public Person(String name, String gender) { this._name = name; this._gdr = gender; } protected String getFullName() { return _name; } protected void setFullName(String value) { _name = value; } protected String getGender() { return _gdr; } protected void setGender(String value) { _gdr = value; } } public class Exercise { public static void main(String[] args) { Person man = new Person(); man.setFullName("Gertrude Binam"); man.setGender("Female"); System.out.println("Staff Member"); System.out.printf("Full Name: %s\n", man.getFullName()); System.out.printf("Gender: %s", man.getGender()); } }
This would produce:
Staff Member Full Name: Gertrude Binam Gender: Female
|
Inheritance
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment