1 / 30

Inheritance

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.

dkozlowski
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance

  2. Agenda • Inheritance • Creating a Subclass • Overriding and overloading • Final keyword

  3. Classes and Inheritance

  4. 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.

  5. SuperclassesandSubclasses

  6. Whyinheritance • To take advantage code reuse. • To use polymorphism.

  7. 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) { this.radius = radius; } /** Return area */ public double findArea() { return radius * radius * Math.PI; } }

  8. Example, cont. // 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

  9. Example, cont. 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

  10. InheritanceHierarchy • 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.

  11. Creating a Subclass

  12. 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.

  13. UsingtheKeywordsuper • 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

  14. Aresuperclass’s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly from subclass. Explicitly using the super keyword. Implicityif super is not used.

  15. 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.

  16. Overriding and overloading

  17. 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.

  18. 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) { this.radius = radius; } /** Return area */ public double findArea() { return radius * radius * Math.PI; } }

  19. 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 }

  20. 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.

  21. 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.

  22. MethodOverloading • 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 { public void eat() { System.out.println("Horse eating hay "); } public void eat(String s) { System.out.println("Horse eating " + s); } }

  23. Overriding vs. Overloading

  24. Visibility modifiers

  25. VisibilityModifiers

  26. VisibilityModifiers

  27. Final keyword

  28. The final Modifier • Thefinalclass 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.

  29. Questions

  30. Thanks

More Related