Inheritance

1,010 views

Published on

Hariprasanna V (9843824677)

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,010
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
55
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. Inheritance in Java CS 3331 Fall 2009
  2. 2. Outline <ul><li>Overloading </li></ul><ul><li>Inheritance and object initialization </li></ul><ul><li>Subtyping </li></ul><ul><li>Overriding </li></ul><ul><li>Hiding </li></ul>
  3. 3. Overloading of Methods and Constructors <ul><li>Def. Overloading </li></ul><ul><ul><li>The ability to allow different methods or constructors of a class to share the same name. </li></ul></ul><ul><ul><li>public class Point { </li></ul></ul><ul><ul><li>public Point() { /* … */ } </li></ul></ul><ul><ul><li>public Point(int x, int y) { /* … */ } </li></ul></ul><ul><ul><li>public double distance(Point other) { /* … */ } </li></ul></ul><ul><ul><li>public double distance(int x, int y) { /* … */ } </li></ul></ul><ul><ul><li>public double distance() { /* … */ } </li></ul></ul><ul><ul><li>// … </li></ul></ul><ul><ul><li>} </li></ul></ul>constructor overloading method overloading
  4. 4. Overloading (Cont.) <ul><li>Which overloaded method to invoke? </li></ul><ul><ul><li>Resolved at compile-time with signature matching , where signature is name and parameter types. </li></ul></ul><ul><ul><li>Constructors/Methods Signatures </li></ul></ul><ul><ul><li>1: Point() Point() </li></ul></ul><ul><ul><li>2: Point(int x,int y) Point(int,int) </li></ul></ul><ul><ul><li>3: double distance(Point other) distance(Point) </li></ul></ul><ul><ul><li>4: double distance(int x,int y) distance(int,int) </li></ul></ul><ul><ul><li>5: double distance() distance() </li></ul></ul><ul><ul><li>Point p1 = new Point(); // which constructor? </li></ul></ul><ul><ul><li>Point p2 = new Point(10,20); </li></ul></ul><ul><ul><li>p2.distance(p1); // which method? </li></ul></ul><ul><ul><li>p2.distance(20,30); </li></ul></ul><ul><ul><li>p2.distance(); </li></ul></ul>
  5. 5. When to Overload? <ul><li>When there is a general, nondiscriminative description of the functionality that fits all the overloaded methods. </li></ul>public class StringBuffer { public StringBuffer append(String str) { /* … */ } public StringBuffer append(boolean b) { /* … */ } public StringBuffer append(char c) { /* … */ } public StringBuffer append(int i) { /* … */ } public StringBuffer append(long l) { /* … */ } // … }
  6. 6. When to Overload? (Cont.) <ul><li>When all the overloaded methods offer the same functionality, with some of them providing default arguments. </li></ul>public class String { public String substring(int i, int j) { // base method: return substring from index i to j - 1. } public String substring(int i) { // provide default argument return substring(i, length()); } // … }
  7. 7. Outline <ul><li>Overloading </li></ul><ul><li>Inheritance and object initialization </li></ul><ul><li>Subtyping </li></ul><ul><li>Overriding </li></ul><ul><li>Hiding </li></ul>
  8. 8. Inheritance <ul><li>Inheritance models the is-a relationship. </li></ul><ul><li>If class S extends class T, then all objects of S can act-like an object of T. </li></ul><ul><li>Only single inheritance is allowed among classes. </li></ul><ul><li>All public and protected members of a superclass are accessible in the subclasses.* </li></ul><ul><li>*All protected members are also accessible within the package. </li></ul>
  9. 9. Constructors of Subclasses <ul><li>Can invoke a constructor of the direct superclass. </li></ul><ul><ul><li>super(…) must be the first statement. </li></ul></ul><ul><ul><li>If the super constructor call is missing, by default the no-arg super() is invoked implicitly. </li></ul></ul><ul><li>Can also invoke another constructor of the same class. </li></ul><ul><ul><li>this(…) must be the first statement. </li></ul></ul>
  10. 10. Example of “this” Calls public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public Point() { // default constructor this.x = 0; this.y = 0; } } this(0,0);
  11. 11. Example of “super” Calls public class ColoredPoint extends Point { private Color color; public ColoredPoint(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; } public ColoredPoint(int x, int y) { this(x, y, Color.BLACK); // point with default value } public ColoredPoint() { color = Color.BLACK; // what will be the values of x and y? } } super(x,y); Point -x: int -y: int +Point() +Point(x: int, y: int)
  12. 12. Default Constructor <ul><li>If no constructor is defined, the following form of no-arg default constructor is automatically generated by the compiler. </li></ul>public ClassName() { super(); } Q. What would be the use of default constructor?
  13. 13. Execution Order of Constructors <ul><li>Rule: Superclass first and field initialization first </li></ul><ul><li>- Instance variable initializer vs. </li></ul><ul><li>instance initializer (or initialization block) </li></ul><ul><li>Example: S x = new S(); </li></ul>public class T { int x = 10; // 1: first public T() { x = 20; // 2: s econd } // ... } public class S extends T { int y = 30; // 3: third public S() { super(); y = 40; // 4: fourth } // ... }
  14. 14. Exercise <ul><li>What value will be printed and why? </li></ul>class Super { protected int x = 100; } class Sub extends Super { { x = 200; } public static void main(String[] args) { System.out.println(new Sub().x); } }
  15. 15. Exercise <ul><li>What are the room number and capacity of the following two objects, respectively? </li></ul><ul><li>ClassRoom c1 = new ClassRom(100); </li></ul><ul><li>ClassRoom c2 = new ClassRoom(300, 100); </li></ul>class ClassRoom extends Room { private int capacity = 60; public ClassRoom(int c) { capacity = c; } public ClassRoom(int n, int c) { super(n); } { if (number % 2 == 1) capacity *= 2; } } class Room { private int number = 100; public Room() { this(++nextNum); } public Room(int n) { number = n; } private static int nextNum = 100; static { nextNum += 100; } { number += 2; } }
  16. 16. Outline <ul><li>Overloading </li></ul><ul><li>Inheritance </li></ul><ul><li>Subtyping </li></ul><ul><li>Overriding </li></ul><ul><li>Hiding </li></ul>
  17. 17. Types <ul><li>What’re types? </li></ul><ul><ul><li>Sets of values </li></ul></ul><ul><li>Why types? </li></ul><ul><ul><li>To statically detect program errors, called type errors . </li></ul></ul><ul><ul><li>Typed- vs. untyped-languages (also, static vs. dynamic typing) </li></ul></ul><ul><li>Types in Java </li></ul><ul><ul><li>Primitive types (e.g., boolean, int, long, etc.) </li></ul></ul><ul><ul><li>Classes (e.g., Object, String, etc.) </li></ul></ul><ul><ul><li>Interfaces (e.g., Runnable, List, etc.) </li></ul></ul><ul><ul><li>Array types (e.g., int[], Object[], List[][], etc.) </li></ul></ul>
  18. 18. Subtyping <ul><li>What’s subtyping? </li></ul><ul><ul><li>Subset relationship among types (e.g., Manager is a subtype (or subset) of Employee) </li></ul></ul><ul><ul><li>Subtype’s values are legitimate values of supertypes. </li></ul></ul><ul><li>Why subtyping? </li></ul><ul><ul><li>Can organizes types into hierarchy, called subype hierarchy </li></ul></ul><ul><ul><li>Can extend programs by adding subtypes </li></ul></ul><ul><li>Subtyping in Java </li></ul><ul><ul><li>Between classes (subclassing) </li></ul></ul><ul><ul><li>Between interfaces (subinterfacing) </li></ul></ul><ul><ul><li>Between classes and interfaces (implementation or realization) </li></ul></ul><ul><ul><li>Between arrays </li></ul></ul>
  19. 19. Substitution Property <ul><li>Def. Substitution property </li></ul><ul><ul><li>A value of subtype can appear where a value of its supertype is expected, e.g., in arguments, results, receivers, and assignments. </li></ul></ul>// 1. argument // public void register( Student s); register(new PhDStudent(“Joe”)); // 2. return value public Student find(String name) { // I am lazy, so I am going to return Joe every time. return new PhDStudent(“Joe”); } // 3. receiver of method call // toString in the class Object new PhDStudent(“Joe”).toString();
  20. 20. Substitution Property (Cont.) <ul><li>Rules of (polymorphic) assignment </li></ul><ul><ul><li>The type of expression at the right-hand side of an assignment must be a subtype of the type of the variable at the left-hand side of the assignment. </li></ul></ul>class Student { … } class Undergraduate extends Student { … } class Graduate extends Student { … } Student s1, s2; s1 = new Undergradute(); // polymorphic assignment s2 = new Graudate(); // polymorphic assignment Graduate s3 = s2; // is this OK? Graduate s3 = (Graduate) s2; // explicit casting
  21. 21. Widening and Narrowing <ul><li>Def. widening and narrowing </li></ul><ul><ul><li>The conversion of a subtype to one of its supertype is called widening , and the conversion of a supertype to one of its subtype is called narrowing (or downcasting ). </li></ul></ul>// s is a stack of strings Stack s = new Stack(); s.push(“Hello”); // widening: void push(Object) … // Stack defines a method top, i.e., “public Object top()”. s.top().size(); // okay? ((String) s.top()).size(); // downcasting
  22. 22. Outline <ul><li>Overloading </li></ul><ul><li>Inheritance </li></ul><ul><li>Subtyping </li></ul><ul><li>Overriding </li></ul><ul><li>Hiding </li></ul>
  23. 23. Overriding Methods <ul><li>Def. Overriding </li></ul><ul><ul><li>Refers to the introduction of an instance method in a subclass that has the same name, signature, and return type of a method declared in the superclass. </li></ul></ul><ul><li>Consequences </li></ul><ul><ul><li>Implementation of the method in the subclass replaces the implementation of the method in the superclass </li></ul></ul>
  24. 24. Overriding Methods (Cont.) public class T { public void m() { … } } public class S extends T { public void m() { … } } T t = new T(); S s = new S(); t.m(); // invoke m of class T s.m(); // invoke m of class S
  25. 25. Overriding Methods (Cont.) <ul><li>Dynamic dispatch (binding): The method to be invoked is determined at runtime by the runtime type of the object, not by the declared type (static type). </li></ul>class Student { public int minCredits() { return 12; } … } class GraduateStudent extends Student { public int minCredits() { return 9; } … } Student s; // … s.minCredits(); // which minCredits method?
  26. 26. Overriding Methods (Cont.) <ul><li>Q. How overriding differ from overloading? </li></ul>
  27. 27. Implementation of Dynamic Binding <ul><li>Storage structure of instance variables </li></ul><ul><ul><li>Class instance records (CIRs) store the state of an object </li></ul></ul><ul><ul><li>The CIR for a subclass adds its new fields to the parent CIR. </li></ul></ul><ul><li>Dynamic bindings of messages to methods </li></ul><ul><ul><li>Virtual Method Tables (VMTs) are used for dynamic binding. </li></ul></ul>
  28. 28. Dynamic Binding (Cont.) class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } } class ColoredPoint extends Point { private Color color; public ColoredPoint(int x, int y, Color c) { super(x, y); color = c; } public Color getColor() { return color; } } x y … x y color … CIR for ColoredPoint CIR for Point
  29. 29. Dynamic Binding (Cont.) ColoredPoint p = new ColoredPoint(10,20,Color.RED); p.getColor(); p.getX(); Q: What if getX is overridden in ColoredPoint? super: vmt: … Point.class getX: getY: … class: x: 10 y: 20 color: p: Color.RED super: vmt: … ColoredPoint.class Object.class super: vmt: … getColor: …
  30. 30. Outline <ul><li>Overloading </li></ul><ul><li>Inheritance </li></ul><ul><li>Subtyping </li></ul><ul><li>Overriding </li></ul><ul><li>Hiding </li></ul>
  31. 31. Hiding Fields and Class Methods <ul><li>Def. Hiding </li></ul><ul><ul><li>Refers to the introduction of a field (instance or class) or a class method in a subclass that has the same name as a field or class method declared in the superclass. </li></ul></ul><ul><li>Hiding vs. overriding </li></ul><ul><ul><li>Statically resolved (bound) at compile-time vs. dynamically dispatched at run-time </li></ul></ul>
  32. 32. Example class Student { protected String description = “Student”; public String getDescription() { return description; } } class Undergraduate extends Student { protected String description = “Undergraduate”; } new Student().getDecription(); // what value is returned? new Undergraduate().getDescription(); // what value? Q. How to refer to hidden fields? hiding of field

×