Download presentation
Presentation is loading. Please wait.
1
CIS 368/568 BU209 Brian O’Haire MCSE MCDBA BU322 Offices Hours 7-8 before class Other times by appointment Email: prof@cdpswat.comprof@cdpswat.com Web cis.csuohio.edu/~brohaire
2
Constructor Chaining Constructor is called whenever an instance is of a class is created. Constructor is called whenever an instance of a subclass is created.
3
How constructor is determined A class can have no explicit constructors Java generates implicit constructor with no explicit arguments The implicit constructor is not visible to subclasses. A class can have many constructors, but each must have a different number of arguments
4
Example of multiple constructors 1) public class Circle { 2) public double r; 3) // The constructor methods 4) public Circle(double r) { this.r = r; } 5) public Circle() {r = 0;} 6) }
5
Constructor chain If a subclass’s first statement does not call a constructor in the first line, Java implicitly inserts super(); Subclass can call constructor with this() super() each of these may have arguments super() will not find the implicit superclass constructor and generate syntax error
6
Process of instantiating First line of constructor of subclass is call to superclass This implies that all superclass variables will be initialized prior to executing the code of the constructor.
7
Superclass of PlaneCircle 1) public class Circle { 2) public double r; 3) // The constructor methods 4) public Circle(double r) { this.r = r; } 5) public Circle() {r = 0;} 6) }
8
Subclass of Circle 1) public class PlaneCircle extends Circle { 2) public double cx, cy; r 3) public PlaneCircle(double d, double x, double y) { 4) super(d/2); // 5) this.r = super.r; 6) this.cx = x; // 7) this.cy = y; // 8) } 9) }
9
Method Signature Signature differentiates methods Signature consists of method name number and type of parameters Not part of signature name of parameters return type type of exceptions thrown
10
Shadowing superclass fields A subclass shadows a superclass when the classes have fields with the same name. There are qualifications that allow you to access the fields in the superclass this super casts
11
Shadow Example 1/5 public class Test{ public static void main(String[] args) { int i1 = 10; A a = new A(1); B b = new B(2); C c = new C(3); System.out.println("main"); System.out.println("a.r = " + a.r); System.out.println("b.r = " + b.r); System.out.println("((A)c).r =" + ((A)c).r); System.out.println("end of class"); System.exit(0); }
12
Shadow Example 2/5 1) public class A { 2) public int r; 3) public A(){ } 4) public A(int r) 5) {this.r = r;} 6) }
13
Shadow Example 3/5 1) public class B extends A { 2) public int r; 3) public B() { 4) super(); 5) } 6) public B(int r) { 7) super(r*2); 8) this.r = r; 9) } 10) }
14
Shadow Example 4/5 1) public class C extends B { 2) public int r; 3) public C() { 4) 5) super(); 6) } 7) public C(int r) { 8) super(r*2); 9) this.r = r; 10) System.out.println("r = " + r); 11) System.out.println("this.r = " + this.r); 12) System.out.println("super.r = " + super.r); 13) System.out.println("((B)this).r = " + ((B)this).r); 14) System.out.println("((A)this).r = " + ((A)this).r); 15) } 16) }
15
Shadow Example 5/5 r = 3 this.r = 3 super.r = 6 ((B)this).r = 6 ((A)this).r = 12 main a.r = 1 b.r = 2 ((A)c).r =12 end of class
16
Overriding superclass methods When a subclass has the a method with the same signature and return type as the superclass is overriding This is not shadowing This is not overloading Illegal for a class method to shadow an instance method
17
Method overloading - fragment Methods with same name but different signature is overloading 1) public class A { 2) public f() {…} 3) } 4) public class B extends A { 5) public f(int r) {…} 6) }
18
Method overriding - fragment Methods with same name but different signature is overloading 1) public class A { 2) public f() {…} 3) } 4) public class B extends A { 5) public f() {…} 6) }
19
Override example public class A { int i = 1; int f() {return 1;} static char g() {return 'A';} }
20
Override example public class B extends A { int i = 2; int f() {return -1;} static char g(){return 'B';} }
21
Override example Override examplepublic class Test{ public static void main(String[] args) { int i1 = 10; B b = new B(); System.out.println("b.i" + b.i); System.out.println("b.f() = " + b.f()); System.out.println("b.g() = " + b.g());
22
Override example System.out.println("B.g() = " + B.g()); A a = (A) b; System.out.println("a.i" + a.i); System.out.println("a.f() = " + a.f()); System.out.println("a.g() = " + a.g()); System.out.println("A.g() = " + A.g()); System.out.println("end of class"); System.exit(0); } }
23
Override example b.i2 b.f() = -1 b.g() = B B.g() = B a.i1 a.f() = -1 a.g() = A A.g() = A end of class
24
Dynamic method lookup Occurs at runtime If circle has subclass eclipse and both have a method area, at runtime the proper method is automatically selected depending on OJBECT. Slides see JHTP chapter 10 slides 35-50
25
Data Hiding and Encapsulation Encapsulation – hiding data in class and making it available through methods Encapsulation – also hides internal methods. Best practice for OO programming
26
Encapsulation Reasons for using: Prevents other users from modifying your internal data without following conventions/edits Allows better control of external interface of class Easier to fix/expand class without breaking programs already using extending/using class Makes testing easier/better defined Makes cleaner API Makes easier documentation
27
Encapsulation Slides see JHTP chapter 10 slides 35-50
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.