



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of inheritance, overloading, overriding, dynamic binding, and hiding in java. It covers the concepts of method overloading and constructor overloading, the reasons for using them, and the rules for method selection. The document also explains the concept of dynamic binding and its implementation in java, as well as the differences between hiding and overriding. Furthermore, it discusses the execution order of constructors and the rules for polymorphic assignment. Essential for university students studying java programming, particularly those in advanced courses on object-oriented programming.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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() { / … */ } // … }
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) { / … */ } // … }
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()); } // … }
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
Constructors of Subclasses
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
Substitution Property (Cont.)
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
// 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