330 likes | 487 Views
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) { /* … */ }
E N D
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) { /* … */ } • public double distance(int x, int y) { /* … */ } • public double distance() { /* … */ } • // … • }
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) { /* … */ } // … }
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()); } // … }
How does Java figure out which method to use? • new StringBuffer sb = append( …);
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();
Overriding Methods • Def. Overriding • Consequences
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
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?
Implementation of Dynamic Binding • Storage structure of instance variables • Dynamic bindings of messages to methods
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; } }
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();
Hiding Fields and Class Methods • Def. Hiding
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 vs. overriding • Statically resolved (bound) at compile-time vs. dynamically dispatched at run-time
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);
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.
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.
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; } }
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? } }
Default Constructor public ClassName() { super(); } Q. What would be the use of default constructor?
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 } // ... }
Types • What is a “type”?
Subtyping • What’s subtyping?
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.
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
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