Constructors under inheritance Variable Shadowing

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

Object Oriented Programming with Java
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Based on OOP with Java, by D.J. Barnes 1 Review 4 View classes as modules Encapsulate operations 4 View classes as struct types Encapsulate data 4 View.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Intro to OOP with Java, C. Thomas Wu
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Inheritance.
Modern Programming Tools And Techniques-I
GC211 Data structure Lecture 3 Sara Alhajjam.
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Java Inheritance.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Modern Programming Tools And Techniques-I Inheritance
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
CSC 113 Tutorial QUIZ I.
Java Inheritance.
Inheritance.
METHOD OVERRIDING in JAVA
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Key Concepts from 1301: What you should already know
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

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

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

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

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

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

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

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

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 2000.0 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