Inheritance
Agenda Inheritance Creating a Subclass Overriding and overloading Final keyword
Classes and Inheritance
Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. Inheritance represents the is a relationship, an object of a subclass also can be treated as an object of its superclass.
Superclasses and Subclasses
Why inheritance To take advantage code reuse. To use polymorphism.
Example // Circle.java: Class definition for describing Circle public class Circle { private double radius; public Circle() {} public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; /** Set a new radius */ public void setRadius(double radius) { /** Return area */ public double findArea() { return radius * radius * Math.PI;
Example, cont. UML Diagram // Cylinder.java: Class definition for describing Cylinder public class Cylinder extends Circle { private double length = 1; /** Return length */ public double getLength() { return length; } /** Set length */ public void setLength(double length) { this.length = length; /** Return the volume of this cylinder */ public double findVolume() { return findArea() * length; UML Diagram
Example, cont. The output is: Cylinder cylinder = new Cylinder(); System.out.println("The length is " + cylinder.getLength()); System.out.println("The radius is " + cylinder.getRadius()); System.out.println("The volume of the cylinder is " + cylinder.findVolume()); System.out.println("The area of the circle is " + cylinder.findArea()); The output is: The length is 1.0 The radius is 1.0 The volume of the cylinder is 3.14159 The area of the circle is 3.14159
Inheritance Hierarchy A class may have several subclasses and each subclass may have subclasses of its own. The collection of all subclasses descended from a common ancestor is called an inheritance hierarchy.
Creating a Subclass
Creating a Subclass Creating a subclass extends properties and methods from the superclass. You can also: Add new properties Add new methods Override the methods of the superclass.
Using the Keyword super The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor To call a superclass method
Are superclass’s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly from subclass. Explicitly using the super keyword. Implicity if super is not used.
CAUTION You must use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor.
Overriding and overloading
Overriding Methods A subclass inherits methods from a superclass. Sometimes it is necessary to modify the method defined in the superclass. This is referred to as method overriding.
Overriding Methods public class Circle { private double radius; public Circle() {} public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; /** Set a new radius */ public void setRadius(double radius) { /** Return area */ public double findArea() { return radius * radius * Math.PI;
Overriding Methods // Cylinder.java: New cylinder class that overrides the findArea() // method defined in the circle class. public class Cylinder extends Circle { /** Return the surface area of this cylinder. The formula is * 2 * circle area + cylinder body area */ public double findArea() { return 2 * super.findArea() + 2 * getRadius() * Math.PI * length; } // Other methods are omitted
Overriding a method rules The argument list must exactly match.(If they don't match, call it overloaded). The return type must be the same. The access level can't be more restrictive. The access level can be less restrictive.
Overriding a method rules, cont. Instance methods can be overridden only if they are inherited. A subclass within the same package can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked public or protected. You cannot override a method marked final.
Method Overloading Method overloading: having multiple methods with the same name but different signatures in a class. You can overload superclass method public class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } public class Horse extends Animal { System.out.println("Horse eating hay "); public void eat(String s) { System.out.println("Horse eating " + s);
Overriding vs. Overloading
Visibility modifiers
Visibility Modifiers
Visibility Modifiers
Final keyword
The final Modifier The final class cannot be extended: final class Math { ... } The final variable is a constant: final static double PI = 3.14159; The final method cannot be overridden by its subclasses.
Questions
Thanks