1 / 30

Inheritance in Java

Inheritance in Java. CS 3331. Two Programming Languages Words . Overloading Overriding. Overloading of Methods and Constructors. public class Point { public Point() { /* … */ } public Point(int x, int y) { /* … */ } public double distance(Point other) { /* … */ }

nuwa
Download Presentation

Inheritance in Java

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 in Java CS 3331

  2. Two Programming Languages Words • Overloading • Overriding

  3. Overloading of Methods and Constructors • public class Point { • public Point() { /* … */ } • public Point(int x, int y) { /* … */ } • public double distance(Point other) { /* … */ } • public double distance(int x, int y) { /* … */ } • public double distance() { /* … */ } • // … • }

  4. Why use overloading?

  5. Why use overloading? 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. When to Overload? (Cont.) 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. How does Java figure out which method to use? • new StringBuffer sb = append( …);

  8. Overloading • 1: Point() Point() • 2: Point(int x,int y) Point(int,int) • 3: double distance(Point other) distance(Point) • 4: double distance(int x,int y) distance(int,int) • 5: double distance() distance() • Point p1 = new Point(); // which constructor? • Point p2 = new Point(10,20); • p2.distance(p1); // which method? • p2.distance(20,30); • p2.distance();

  9. Overriding Methods • Def. Overriding • Consequences

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

  11. What value is returned? class Student { public int maxCredits() { return 15; } … } class GraduateStudent extends Student { public int maxCredits() { return 12; } … } Student s; // … s.getMaxCredits(); // which maxCredits method?

  12. Implementation of Dynamic Binding • Storage structure of instance variables • Dynamic bindings of messages to methods

  13. 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; } }

  14. Object.class Point.class p: getX: getY: … super: vmt: … super: vmt: … super: vmt: … ColoredPoint.class class: x: 10 y: 20 color: getColor: … Color.RED Dynamic Binding (Cont.) ColoredPoint p = new ColoredPoint(10,20,Color.RED); p.getColor(); p.getX();

  15. Overriding Methods (Cont.)

  16. Hiding Fields and Class Methods • Def. Hiding

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

  18. Hiding vs. overriding • Statically resolved (bound) at compile-time vs. dynamically dispatched at run-time

  19. Another Example class Point { public String description = “Point”; } class CPoint extends Point { public String description = “CPoint”; } CPoint c1 = new CPoint(); Point c2 = c1; system.out.println (c1.description); system.out.println (c2.description);

  20. Inheritance • Inheritance models the is-a relationship. • If class S extends class T, then all objects of S can act-like an object of T. • In Java, only single inheritance is allowed among classes. • All public and protected members of a superclass are accessible in the subclasses.* *All protected members are also accessible within the package.

  21. Constructors of Subclasses • Can invoke a constructor of the direct superclass. • super(…) must be the first statement. • If the super constructor call is missing, by default the no-arg super() is invoked implicitly. • Can also invoke another constructor of the same class. • this(…) must be the first statement.

  22. 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; } }

  23. 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? } }

  24. Default Constructor public ClassName() { super(); } Q. What would be the use of default constructor?

  25. Execution Order of Constructors public class T { int x = 10; // first public T() { x = 20; // second } // ... } public class S extends T { int y = 30; // third public S() { super(); y = 40; // fourth } // ... }

  26. Types • What is a “type”?

  27. Subtyping • What’s subtyping?

  28. Substitution Property • Def. Substitution property • A value of subtype can appear where a value of its supertype is expected, e.g., in arguments, results, receivers, and assignments.

  29. Substitution Property (Cont.) • Rules of (polymorphic) assignment • 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. class Student { … } class Undergraduate extends Student { … } class Graduate extends Student { … } Student s1, s2; s1 = new Undergradute(); // polymorphic assignment s2 = new Graduate(); // polymorphic assignment Graduate s3 = s2; // is this OK? Graduate s3 = (Graduate) s2; // explicit casting

  30. Widening and Narrowing • Def. widening and narrowing • The conversion of a subtype to one of its supertypes is called widening, and the conversion of a supertype to one of its subtype is called narrowing (or downcasting). // s is a stack of strings Stack s = new Stack(); s.push(“Hello”); … // Stack defines a method top, i.e., “public Object top()”. s.top().size(); // okay? ((String) s.top()).size(); // downcasting

More Related