Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors under inheritance Variable Shadowing

Similar presentations


Presentation on theme: "Constructors under inheritance Variable Shadowing"— Presentation transcript:

1 Constructors under inheritance Variable Shadowing
Inheritance II Constructors under inheritance Variable Shadowing 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

2 Inheritance & constructors (c) Eraj Basnayake
Let there be light…Constructor chaining under inheritance class Light{ protected int noOfWatts; //wattage protected boolean indicator; //on/off protected String location; //placement private static int counter=0; //no of light objects public Light(){ this(0,false); } public Light(int noOfWatts, boolean indicator){ setNoOfWatts(noOfWatts); if (indicator) switchOn(); else switchOff(); public void switchOn() {indicator = true;} public void switchOff() {indicator = false;} public boolean isOn() { return indicator;} public void setNoOfWatts(int watts){… } public static void writeCounter(){ System.out.println(“Number of lights: “ +counter); //… class TubeLight extends Light{ private int tubeLength; private int color; public TubeLight(){ setTubeLength(3); setColor(1); } public TubeLight(int length, int color){ this(length, color,30); public TubeLight(int l, int c, int w){ super(w, false); setTubeLength(length); setColor(color); //… public static void main(String[] args){ TubeLight t1 = new TubeLight(); TubeLight t2 = new TubeLight(3,1); TubeLight t3 = new TubeLight(3,1,50); 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

3 Implicit call to “Light()”
Constructors under inheritance… The keyword super refers to the superclass, from the perspective of the current-class, while the keyword this refers to the current-class. Java’s rule: If first line of constructor is not an explicit call to a superclass (or this) constructor, Java will implicitly put super() as the fist line, calling the superclass’s default constructor. A call to a superclass constructor has to be the first line of code of a constructor, or not at all. public TubeLight(){ setTubeLength(3); setColor(1); } public TubeLight(int length, int color){ this(length, color,30); public TubeLight(int l, int c, int w){ super(w, false); setTubeLength(length); setColor(color); Implicit call to “Light()” None of Super’s constructors will be called from here. Explicit call to “Light(int, int)” 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

4 Inheritance & constructors (c) Eraj Basnayake
Question: if a super class had at least 1 non-default constructor and no default constructor, would a subclass explicitly have to invoke a super class constructor? More “super” and “this” super(xxx) calls a superclass constructor must be first line of a subclass constructor super.xxx accesses superclass variables usable anywhere in the code of a sub class super.xxx(xxx) calls a superclass method usable anywhere in the code of a sub class this(xxx) calls a current-class constructor must be first line of current class constr. this.xxx accesses a current-class variable usable anywhere in the code this.xxx(xxx) calls a current-class method usable anywhere in the code super.super.xxx invalid statement invalid statement 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

5 Important Difference between methods and variables under inheritance
Variable Shadowing A subclass cannot override variables of a superclass - but it can shadow them. That means, it can have its own variable of the same type as the superclass without a conflict with that of the superclass. A subclass can use super to access the inherited member variable. When a method is invoked using a reference it is the class of the current object , not the reference that determines which method to run. However, in the case of a variable, it is the class of the reference, not the class of the current object denoted by the reference, that determines which variable will actually be accessed Important Difference between methods and variables under inheritance 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

6 Inheritance & constructors (c) Eraj Basnayake
Example: Remember the class “Light”, Tubelight, … shadowing public class Light{ public String billType = “Small bill” //… protected double getBill(int noOfHours){ double smallAmount = 10.0; smallAmount = smallAmount * noOfHours; System.out.println(billType+”:”+smallAmount); return smallAmount; } public void banner(){ System.out.println(“Let there be light!”); public class TubeLight extends Light{ public String billType = “Large bill”; //… public double getBill(int noOfHours){ double largeAmount = 100.0; largeAmount = largeAmount * noOfHours; System.out.println(billType+”:”+largeAmount); return largeAmount; } public double getBill(){ System.out.println(“no bill”); return 0.0; 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

7 Inheritance & constructors (c) Eraj Basnayake
class NeonLight extends TubeLight{ //… public void demonstrate(){ this.banner(); this.getBill(20); this.getBill(); System.out.println(this.billType); TubeLight tl = this; tl.banner(); tl.getBill(20); tl.getBill(); System.out.println(tl.billType); Light l = this; l.banner(); l.getBill(20); l.getBill(); System.out.println(l.billType); } public class Client{ public static void main(String args[]){ NeonLight neonRef = new NeonLight(); neonRef.demonstrate(); What’s the output? Why? 2/17/2019 Inheritance & constructors (c) Eraj Basnayake

8 Inheritance & constructors (c) Eraj Basnayake
class NeonLight extends TubeLight{ //… public void demonstrate(){ this.banner(); this.getBill(20); this.getBill(); System.out.println(this.billType); super.banner(); super.getBill(20); super.getBill(); System.out.println(super.billType); ((Light) this).banner(); ((Light) this).getBill(20); ((Light) this).getBill(); System.out.println(((Light) this).billType); } public class Client{ public static void main(String args[]){ NeonLight neonRef = new NeonLight(); neonRef.demonstrate(); what’s going on here? Now can you tell what the output is and why? Output Let there be light! Large bill No bill Large bill <compiler Error> Small bill Reasoning Inheritance Method Overriding Extending the super class Most local variable Method overriding Variable Shadowing Can only access methods that are in Light 2/17/2019 Inheritance & constructors (c) Eraj Basnayake


Download ppt "Constructors under inheritance Variable Shadowing"

Similar presentations


Ads by Google