Chapter 8 Inheritance


Published on

Chapter 8 Inheritance
Taught by Oum Saokosal, Head of Information Technology, National Polytechnic Institute of Cambodia

Published in: Technology, News & Politics
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter 8 Inheritance

  1. 1. Chapter 8 Inheritance and Polymorphism Oum Saokosal , Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail:
  2. 2. Inheritance Chapter 8 Inheritance and Polymorphism
  3. 3. Inheritance <ul><li>What is Inheritance? </li></ul><ul><li>Why Inheritance? </li></ul><ul><li>How to use it? </li></ul><ul><li>Superclass & Subclass </li></ul><ul><li>Using keyword super </li></ul><ul><li>Overriding Methods </li></ul><ul><li>The Object class </li></ul>
  4. 4. 1. What is Inheritance?
  5. 5. 1. What is Inheritance? (1) <ul><li>OOP has 3 features: </li></ul><ul><ul><li>Class Encapsulation </li></ul></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul><li>Polymorphism </li></ul></ul><ul><li>OOP allows you to derive (create) new objects from existing classes. E.g. </li></ul><ul><ul><li>You can create objects from a class: </li></ul></ul><ul><ul><ul><li>Circle cir = new Circle(); </li></ul></ul></ul><ul><ul><ul><li>Word w = new Word(“N P I C”); </li></ul></ul></ul>
  6. 6. 1. What is Inheritance? (2) <ul><li>But OOP has other mechanisms. One of them is called Inheritance . </li></ul><ul><li>Inheritance is a mechanism to make classes inherit properties/methods from an existing class. </li></ul><ul><li>Inherit (v) ¬TTYlekrþtMENl¦ </li></ul><ul><li>Inheritance (n) receiving properties </li></ul>
  7. 7. 1. What is Inheritance? (3) <ul><li>In fact, every class in Java is always inherited from an existing class, either explicitly or implicitly. </li></ul><ul><li>In Java, every class is inherited from java.lang.Object . </li></ul><ul><li>To be clear, please look at an example at next slide. </li></ul>
  8. 8. 1. What is Inheritance? (4) - Example <ul><li>Please create a blank class, say, BlankSample </li></ul><ul><ul><li>public class BlankSample { </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Then create a test class, say, TestBlank </li></ul><ul><ul><li>public class TestBlank { </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>BlankSample bs = new BlankSample(); </li></ul></ul><ul><ul><li>System.out.print( bs.toString() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>The question is why we can call bs.toString()? </li></ul><ul><li>If we look at BlankSample, there is toString(). Why? </li></ul>
  9. 9. 1. What is Inheritance? (5) - IDE
  10. 10. 1. What is Inheritance? (6) <ul><li>Where these methods come from? </li></ul><ul><li>They are from java.lang.Object . Because every class in Java inherits from java.lang.Object . </li></ul><ul><li>To be sure, please look at the API and find out java.lang.Object. Then see its methods. </li></ul><ul><ul><li>clone(), equals(Object obj), finalize(), getClass(), hashCode(), notify(), </li></ul></ul><ul><ul><li>notifyAll(), toString() and wait() </li></ul></ul>
  11. 11. 2. Why Inheritance?
  12. 12. 2. Why Inheritance? <ul><li>Classes often share capabilities </li></ul><ul><li>We want to avoid re-coding these capabilities </li></ul><ul><li>Reuse of these would be best to </li></ul><ul><ul><li>Improve maintainability </li></ul></ul><ul><ul><li>Reduce cost </li></ul></ul><ul><ul><li>Improve “real world” modeling </li></ul></ul>
  13. 13. 2. Why Inheritance? -Benefits <ul><li>No need to reinvent the wheel. </li></ul><ul><li>Allows us to build on existing codes without having to copy it and past it or rewrite it again, etc. </li></ul><ul><li>To create the subclass, we need to program only the differences between the superclass and the subclass that inherits from it. </li></ul><ul><li>Make class more flexible. </li></ul>
  14. 14. 3. How to use it?
  15. 15. 3. How to use it? (1) <ul><li>In Java, to enable a class inherit an existing class, we have to use a keyword “ extends ”. For example, we have Circle class: </li></ul><ul><ul><li>public class Circle{ </li></ul></ul><ul><ul><li>private double radius; </li></ul></ul><ul><ul><li>public Circle(){} </li></ul></ul><ul><ul><li>public Circle(double radius){ </li></ul></ul><ul><ul><li>this.radius = radius; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void setRadius (double radius){ </li></ul></ul><ul><ul><li>this.radius = radius; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public double findArea (){ </li></ul></ul><ul><ul><li>return radius * radius *3.14; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  16. 16. 3. How to use it? (2) <ul><li>Then we want another class, say, TestCircle , inherits from the Circle class. </li></ul><ul><ul><li>public class TestCircle extends Circle{ </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>TestCircle tc1 = new TestCircle (); </li></ul></ul><ul><ul><li>tc1. setRadius (5.0); </li></ul></ul><ul><ul><li>System.out.println(tc1. findArea ()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Please note that TestCircle didn’t define setRadius() and getArea() methods but it could use the methods. </li></ul><ul><li>The reason is TestCircle inherits from Circle class. </li></ul>
  17. 17. 3. How to use it? – Note (1) <ul><li>Usually inheritance is used to improve features of an existing class. </li></ul><ul><li>Please look at the code on page 288, listing 8.1 First Version of the Cylinder class. </li></ul><ul><ul><li>The Circle has already the findArea() </li></ul></ul><ul><ul><li>So the formula to find Cylinder’s Volume is : </li></ul></ul><ul><ul><ul><li>volume = Area * length </li></ul></ul></ul>
  18. 18. 3. How to use it? – Note (2) <ul><ul><li>public class Cylinder extends Circle { </li></ul></ul><ul><ul><li>private double length = 1; </li></ul></ul><ul><ul><li>public double getLength(){ </li></ul></ul><ul><ul><li>return length; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void setLength(double length){ </li></ul></ul><ul><ul><li>this.length = length; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public double findVolume(){ </li></ul></ul><ul><ul><li>return findArea() * length; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  19. 19. 3. How to use it? – Note (3) <ul><ul><li>public class TestCylinder { </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>Cylinder c1 = new Cylinder(); </li></ul></ul><ul><ul><li>c1. setRadius (2.5); // from Circle </li></ul></ul><ul><ul><li>c1.setLength(5); // from Cylinder </li></ul></ul><ul><ul><li>System.out.println(c1.findVolume()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Please note that the cylinder’s object, c1, could call a method, “ setLength() ”, from Cylinder class and also could call a method, “ setRadius() ”, from Circle class. </li></ul>
  20. 20. 4. Superclass & Subclass
  21. 21. 4. Superclass & Subclass (1) <ul><li>The cylinder class inherits features from circle class. Then, </li></ul><ul><ul><li>Cylinder is subclass </li></ul></ul><ul><ul><li>Circle is superclass </li></ul></ul><ul><ul><li>Super inherit Subclass </li></ul></ul>Circle Cylinder
  22. 22. 4. Superclass & Subclass (2) <ul><li>Quick Check: </li></ul><ul><ul><li>C1 <- C2 <- C3 <- C4 </li></ul></ul><ul><li>What are superclass and subclass? </li></ul><ul><li>C1 is the superclass of C2, C3, & C4 </li></ul><ul><li>C2 are the subclass of C1 and the superclass of C3 & C4 </li></ul><ul><li>C3 are the subclass of C1 & C2 and the superclass of C4 </li></ul><ul><li>C4 is the subclass of C1, C2 & C3 </li></ul><ul><li>It means if we call the final subclass, e.g. C4, then we can use features from C1, C2, C3, and, of course, C4 itself. </li></ul>
  23. 23. 4 . Superclass & Subclass (3) – Java API <ul><li>Please check API Documentation: Javax.swing.JFrame is the subclass of Frame,Window,Container,Component,Object . So if we use JFrame, it means we use features from all of the superclasses. </li></ul>
  24. 24. 4. Superclass & Subclass (4) <ul><li>Sample of using JFrame </li></ul><ul><ul><li>import javax.swing.*; </li></ul></ul><ul><ul><li>public class TestJFrame extends JFrame { </li></ul></ul><ul><ul><li>public static void main( String [] args){ </li></ul></ul><ul><ul><li>TestJFrame frame = new TestJFrame(); </li></ul></ul><ul><ul><li>frame .setTitle ( &quot;Hi I am JFrame&quot; ); </li></ul></ul><ul><ul><li>frame .setSize (400,300); </li></ul></ul><ul><ul><li>frame .setVisible ( true ); </li></ul></ul><ul><ul><li> frame .setDefaultCloseOperation ( </li></ul></ul><ul><ul><li>JFrame.EXIT_ON_CLOSE); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} // Note the underline codes </li></ul></ul>
  25. 25. 5. Using keyword super
  26. 26. 5. Using keyword super (1) <ul><li>super is used to call: </li></ul><ul><ul><li>Constructors of the superclass </li></ul></ul><ul><ul><li>Methods of the superclass </li></ul></ul>
  27. 27. Using keyword super (2) <ul><li>To call constructors of the superclass </li></ul><ul><ul><li>super(); //call no-arg constructor </li></ul></ul><ul><ul><li>super(5.0); //call arg constructor </li></ul></ul><ul><li>Note </li></ul><ul><li>super() : </li></ul><ul><ul><li>MUST be written in the 1 st line of subclass constructors </li></ul></ul><ul><ul><li>Cannot be written in other methods </li></ul></ul><ul><ul><li>Is the only way to call superclass constructor. </li></ul></ul>
  28. 28. Using keyword super (3) <ul><li>To call methods of the superclass </li></ul><ul><ul><li>super.setRadius(5); // setRadius(5); </li></ul></ul><ul><ul><li>super.findArea(); </li></ul></ul><ul><ul><li>super.toString(); </li></ul></ul><ul><li>Note: </li></ul><ul><li>This keyword is not always used to call methods from superclass. </li></ul><ul><li>We can call superclass methods by calling directly the methods name. Please look at slide # 14. </li></ul><ul><li>However, super is used not to confuse with the name of the overriding methods. </li></ul>
  29. 29. 6. Overriding Methods
  30. 30. Overriding Methods (1) <ul><li>In the real world: </li></ul><ul><li>Researchers sometimes never invent or find a new thing. In fact, they just improve an existing thing. </li></ul><ul><li>To improve the thing, they just: </li></ul><ul><ul><li>Add new features </li></ul></ul><ul><ul><li>Modify existing features. </li></ul></ul>
  31. 31. Overriding Methods (2) <ul><li>In OOP : </li></ul><ul><li>It is true to the both things above. The inheritance helps us to do these. We can: </li></ul><ul><li>1. Add new methods to existing class </li></ul><ul><li>2. Modify the existing features. It is called Overriding Methods . </li></ul>
  32. 32. Overriding Methods (3) <ul><li>Overriding method is a technique to modify a method in the superclass. </li></ul><ul><li>Overriding method is a method, defined in subclass, which has the same name and return type to a method in superclass. </li></ul><ul><ul><li>For example : </li></ul></ul><ul><ul><li>- The Circle has findArea() but Cylinder doesn’t has it. If we call findArea() , it is always the Circle ’s. </li></ul></ul><ul><ul><li>- But the cylinder can have findArea() for itself. This implementation is called overriding method. </li></ul></ul>
  33. 33. Overriding Methods (3) <ul><li>Please look at the code on page 292, Listing 8.2. </li></ul>
  34. 34. Important Note (1) <ul><li>1. In the subclass, we can invoke accessible things , e.g. public methods or constructor , from the superclass. E.g.: </li></ul><ul><li>- After a class inherits JFrame, then we can call setTitle(), setSize(), setVisible() etc. </li></ul><ul><li>In a constructor of subclass, the non-arg constructor of the superclass is ALWAYS invoked. Let see slide “Important Note (2)”. </li></ul><ul><li>3. A subclass can NEVER inherit a superclass which has no non-arg constructor . Let see slide “Important Note (3)”. </li></ul>
  35. 35. Important Note (2) <ul><li>// Circle class </li></ul><ul><li>public class Circle{ </li></ul><ul><li>private double radius; </li></ul><ul><li>public Circle(){ // non-arg constructor </li></ul><ul><li>radius = 5; </li></ul><ul><li>} </li></ul><ul><li> public double findArea(){ </li></ul><ul><li>return radius * radius * 3.14; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//Test Circle class </li></ul><ul><li>public class TestCircle extends Circle { </li></ul><ul><li>public static void main( String [] args){ </li></ul><ul><li>TestCircle tc = new TestCircle(); </li></ul><ul><li>System .out.println(tc.findArea()); //output: 78.5 </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  36. 36. Important Note (3) <ul><li>// Circle class </li></ul><ul><li>public class Circle{ </li></ul><ul><li>private double radius; </li></ul><ul><li> //It doesn’t have non-arg constructor Here </li></ul><ul><li> public Circle(double radius){ </li></ul><ul><li>this. radius = radius; </li></ul><ul><li>} </li></ul><ul><li> public double findArea(){ </li></ul><ul><li>return radius * radius * 3.14 ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//Test Circle class </li></ul><ul><li>public class TestCircle extends Circle { </li></ul><ul><li>public static void main( String [] args){ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>cannot find symbol symbol: constructor Circle() location: class Circle 1 error
  37. 37. The Object class
  38. 38. The Object class (1) <ul><li>public boolean equals(Object object) </li></ul><ul><li>Indicates whether a object is &quot;equal to&quot; this one. E.g.: </li></ul><ul><ul><li>Circle c1 = new Circle(); </li></ul></ul><ul><ul><li>if(c1.equals(c1)){ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Note: We have to override it to test our comparison. </li></ul><ul><li>public int hashCode() </li></ul><ul><li>Returns a hash code value for the object. see “Java Collection Framework.” </li></ul>
  39. 39. The Object class (2) <ul><li>public String toString() </li></ul><ul><li>Return a string that represents the object. e.g. </li></ul><ul><ul><li>Circle c1 = new Circle(); </li></ul></ul><ul><ul><li>c1.toString(); </li></ul></ul><ul><ul><li>//output: Circle@24efe3 </li></ul></ul><ul><li>Note: We have to override it to display our wise. </li></ul>