Overriding Variables: Shadowing

Similar documents
HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance. Transitivity

Inheritance -- Introduction

OVERRIDING. 7/11/2015 Budditha Hettige 82

Practice for Chapter 11

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II. Inheritance

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

COMP 110/L Lecture 19. Kyle Dewey

Lecture 18 CSE11 Fall 2013 Inheritance

More On inheritance. What you can do in subclass regarding methods:

Inheritance and Polymorphism. CS180 Fall 2007

ECE 122. Engineering Problem Solving with Java

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

CMSC 132: Object-Oriented Programming II

Chapter 7. Inheritance

AP CS Unit 6: Inheritance Notes

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

COMP 110/L Lecture 20. Kyle Dewey

Programming Exercise 14: Inheritance and Polymorphism

Polymorphism. return a.doublevalue() + b.doublevalue();

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CLASS DESIGN. Objectives MODULE 4

Introductory Programming Inheritance, sections

Programming in C# Inheritance and Polymorphism

Data Abstraction. Hwansoo Han

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Exercise: Singleton 1

CS Programming I: Inheritance

CS1150 Principles of Computer Science Objects and Classes

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

JAVA MOCK TEST JAVA MOCK TEST II

Java Fundamentals (II)

INHERITANCE. Spring 2019

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Sorting. Sorting. Selection sort

CS-202 Introduction to Object Oriented Programming

More on Objects in JAVA TM

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Reusing Classes. Hendrik Speleers

What is Inheritance?

CSCE3193: Programming Paradigms

Implements vs. Extends When Defining a Class

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

INHERITANCE AND EXTENDING CLASSES

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Making New instances of Classes

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

CS 251 Intermediate Programming Inheritance

Comp 249 Programming Methodology

QUESTIONS FOR AVERAGE BLOOMERS

Java Review: Objects

Class, Variable, Constructor, Object, Method Questions

C++ Important Questions with Answers

Inheritance: Definition

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Programming overview

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Inheritance Motivation

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Building custom components IAT351

CSE 401/M501 Compilers

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Darrell Bethea June 6, 2011

C08: Inheritance and Polymorphism

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

CISC370: Inheritance

JAVA Programming Language Homework I - OO concept

Introducing C++ David Chisnall. March 15, 2011

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Object-Oriented Programming (Java)

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CS313D: ADVANCED PROGRAMMING LANGUAGE

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object Oriented Programming. Java-Lecture 11 Polymorphism

Arrays Classes & Methods, Inheritance

More on Inheritance. Interfaces & Abstract Classes

Implementing Higher-Level Languages. Quick tour of programming language implementation techniques. From the Java level to the C level.

Full file at Chapter 2 - Inheritance and Exception Handling

CS313D: ADVANCED PROGRAMMING LANGUAGE

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

G Programming Languages - Fall 2012

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

9/10/2018 Programming Data Structures Inheritance

Introduction to Inheritance

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

ITI Introduction to Computing II

Transcription:

Overriding Variables: Shadowing We can override methods, can we override instance variables too? Answer: Yes, it is possible, but not recommended Overriding an instance variable is called shadowing, because it makes the base instance variables of the base class inaccessible. (We can still access it explicitly using super.varname). public class Person { public class Staff extends Person { String name; String name; // // name refers to Staff s name This can be confusing to readers, since they may not have noticed that you redefined name. Better to just pick a new variable name 30

Shadowing example class Base { public Base(){x = 10; public String foo(){return x+""; class Derived extends Base { public Derived(){ x = 20; public String foo(){return (x + "\t" + super.x); Derived d = new Derived(); d.foo(); 31

Shadowing example class Base { public Base(){x = 10; public String foo(){return x+""; class Derived extends Base { public Derived(){ x = 20; public String foo(){return (x + "\t" + super.x); Derived d = new Derived(); d.foo(); 20 10 32

Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; b.foo(); 33

Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; b.foo(); 20 10 34

Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; d.x; b.x; 35

Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; d.x; 20 b.x; 10 36

super and this super: refers to the base class object We can invoke any base class constructor using super( ). We can access data and methods in the base class (Person) through super. E.g., tostring( ) and equals( ) invoke the corresponding methods from the Person base class, using super.tostring( ) and super.equals( ). this: refers to the current object We can refer to our own data and methods using this. but this usually is not needed We can invoke any of our own constructors using this( ). As with the super constructor, this can only be done within a constructor, and must be the first statement of the constructor. Example: public Fraction(int n) { this(n,1); 37

Memory Layout class Base{ private int a; protected int b; protected int c; protected void m1(){ public void m2(){ Base class Child extends Base{ private int d; public void m1(){ public void m3(){ Child The Java Virtual Machine does not mandate any particular internal structure for objects. 38

Memory Layout VTABLE class Base{ private int a; Pointer to m1() protected int b; Pointer to m2() protected int c; protected void m1(){ public void m2(){ Base object Pointer to vtable a b c class Child extends Base{ private int d; public void m1(){ public void m3(){ 39

Memory Layout VTABLE class Base{ private int a; Pointer to m1() protected int b; Pointer to m2() protected int c; protected void m1(){ public void m2(){ Base object Pointer to vtable a b c class Child extends Base{ private int d; public void m1(){ public void m3(){ VTABLE Pointer to m1() Pointer to m2() Pointer to m3() Child object Pointer to vtable a b c d 40

Memory Layout VTABLE class Base{ private int a; Pointer to m1() protected int b; Pointer to m2() protected int c; protected void m1(){ public void m2(){ class Child extends Base{ private int d; public void m1(){ public void m3(){ Each class has one vtable. All objects of the this class shares the vtable. VTABLE Pointer to m1() Pointer to m2() Pointer to m3() Base object Pointer to vtable a b c Child object Pointer to vtable a b c d 41

Inheritance and Private Private members: Child class inherits all the private data of Base class However, private members of the base class cannot be accessed directly Why is this? After you have gone to all the work of setting up privacy, it wouldn t be fair to allow someone to simply extend your class and now have access to all the private information 42

Quiz 5: True/False Excepting Object, which has no superclass, every class has one and only one direct superclass. A. True B. False 43

Quiz5: True/False Excepting Object, which has no superclass, every class has one and only one direct superclass. A. True B. False 44

Quiz 6: class Base { public void foo(){ println("base"); class Derived extends Base { private void foo(){ println("derived"); Base b = new Derived(); b.foo(); A. Base B. Derived C. Compiler Error D. Runtime Error 45

Quiz 6: class Base { public void foo(){ println("base"); class Derived extends Base { private void foo(){ println("derived"); Base b = new Derived(); b.foo(); A. Base B. Derived C. Compiler Error D. Runtime Error It is compiler error to give more restrictive access to a derived class function which overrides a base class function. 46

Quiz 7: class Animal has a subclass Mammal. Which of the following is true: A. Because of single inheritance, Mammal can have no subclasses. B. Because of single inheritance, Mammal can have no other parent than Animal. C. Because of single inheritance, Animal can have only one subclass. D. Because of single inheritance, Mammal can have no siblings. 47

Quiz 7: class Animal has a subclass Mammal. Which of the following is true: A. Because of single inheritance, Mammal can have no subclasses. B. Because of single inheritance, Mammal can have no other parent than Animal. C. Because of single inheritance, Animal can have only one subclass. D. Because of single inheritance, Mammal can have no siblings. 48

Access level Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N 49