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

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

Multiple Inheritance, Abstract Classes, Interfaces

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

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Object-Oriented Programming

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Transitivity

1 Shyam sir JAVA Notes

Object Oriented Programming. Java-Lecture 11 Polymorphism

More on Objects in JAVA TM

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

More Relationships Between Classes

What is Inheritance?

Programming II (CS300)

Programming II (CS300)

Introduction to Java Written by John Bell for CS 342, Spring 2018

Exercise: Singleton 1

Java Fundamentals (II)

Practice for Chapter 11

First IS-A Relationship: Inheritance

Type Hierarchy. Lecture 6: OOP, autumn 2003

Example: Count of Points

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

COMP 110/L Lecture 19. Kyle Dewey

VIRTUAL FUNCTIONS Chapter 10

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

CS-202 Introduction to Object Oriented Programming

Chapter 5 Object-Oriented Programming

CS Programming I: Inheritance

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

Introduction to Programming Using Java (98-388)

Inheritance and object compatibility

Written by John Bell for CS 342, Spring 2018

Inheritance and Polymorphism. CS180 Fall 2007

COMP 110/L Lecture 20. Kyle Dewey

Polymorphism. Arizona State University 1

Lecture 18 CSE11 Fall 2013 Inheritance

Java: introduction to object-oriented features

C++ Important Questions with Answers

Comp 249 Programming Methodology

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

25. Generic Programming

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Logistics. Final Exam on Friday at 3pm in CHEM 102

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Programming in C# Inheritance and Polymorphism

Inheritance -- Introduction

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

Chapter 14 Abstract Classes and Interfaces

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

15CS45 : OBJECT ORIENTED CONCEPTS

The major elements of the object-oriented model

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

CS 251 Intermediate Programming Inheritance

Class, Variable, Constructor, Object, Method Questions

Modern Programming Languages. Lecture Java Programming Language. An Introduction

INHERITANCE. Spring 2019

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Inheritance and Polymorphism

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Instance Members and Static Members

ECE 122. Engineering Problem Solving with Java

Inheritance. The Java Platform Class Hierarchy

Polymorphism and Interfaces. CGS 3416 Spring 2018

Full file at Chapter 2 - Inheritance and Exception Handling

Data Structures and Other Objects Using C++

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Inheritance, Polymorphism, and Interfaces

Object-Oriented Concepts

Overriding Variables: Shadowing

Fundamentals of Object Oriented Programming

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Object Orientated Programming Details COMP360

Declarations and Access Control SCJP tips

Week 5-1: ADT Design

Inheritance Motivation

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

ITI Introduction to Computing II

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 5. Inheritance

Chapter 9 Inheritance

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

CLASS DESIGN. Objectives MODULE 4

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

Compaq Interview Questions And Answers

Implements vs. Extends When Defining a Class

Inheritance and Testing Spring 2018 Exam Prep 4: February 11, 2019

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Transcription:

Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either extend another class or implement an interface, but not both. B. Java classes may extend another class and/or implement an interface, but no more than one of either. C. Java classes may extend at most one other class, and may implement as many interfaces as they wish. D. Java classes may extend as many classes as they wish, but may not implement more than one interface. E. Java classes may extent as many classes as they wish, and may implement as many interfaces as they wish. 1

Java Inheritance Basics Java implements inheritance with extends keyword. Java classes may only extend from one parent. Java classes with no specified parent inherit from Object. Abstract classes dictate required methods, but do not provide ( all of ) them. They must be extended before they can be instantiated. Interfaces are like (pure) abstract classes. Java classes may implement as many Interfaces as desired. class Parent { int vara; Inheritance and Scope ( Shadowed Variables ) // super.vara class Child extends Parent { double vara; // this.vara method( ) { float vara; // unqualified vara // Note: super, this not always needed. 2

Method Overriding @Override marker The @Override marker notifies the compiler that the intention is to override an inherited method, not to create a new overloaded method. Compiler will generate error messages if the method does not exist in an ancestor class. class Cat extends Mammal {... @Override void sleep() {... 3

Overriding Methods versus Shadowing Variables If a superclass variable is used to reference a subclass object, access to variables will get the ones defined in the superclass, but access to methods will get the ones in the subclass. Cat simon = new Cat(); Animal creature = simon;... creature.sleep(); // accesses Cat sleep(); creature.weight = 1; // accesses Animal.weight ( Variables uses static binding at compile time; Methods use dynamic binding at run time. ) More Binding Issues Overloaded methods are bound statically at compile time. Overridden methods are bound dynamically at run time. ( Keyword virtual in C++ ) Static methods are bound statically at compile time. Static methods in subclasses can shadow static methods in superclasses ( if not declared final ), only when accessed through a class variable. Superclass method is always available through superclass name. A static method may not be overridden with a non-static method. 4

Exceptions and Overridden Methods A subclass may refine the throws clause in an overriding method to a subtype of the Throwable type specified by its ancestor. ( It may not throw things its ancestor didn t, but it may specify a subset of its ancestor s throws list. ) Callers of the method accessing via a subclass variable only have to handle the subset of Throwables specified in the subclass throws clause, not the full set specified by the ancestor. Return Types and Overridden Methods A subclass may refine the return type in an overriding method to a subtype of the return type specified by its ancestor. ( It may return a refined version of what its ancestor returned. ) class Animal { Animal create() {... class Mammal extends Animal { Mammal create() {... // A Mammal IS A Animal 5

this and super revisited class GrandParent { int vara; // super.super.vara?? Not allowed with methods int varb; // super.varb class Parent { float vara; // super.vara class Child extends Parent { double vara; // this.vara double varb; // unqualified varb method( ) { float vara; // unqualified vara Casting Upcasting to an ancestor type is safe. Downcasting to a descendant type is dangerous. Check using instanceof before downcasting. Errors will result from improper casting. Casting can affect selection of statically bound members, but not dynamically bound ones. ( See next Slide ) 6

Casting and selection of methods and variables Review of this and super in constructors Every descendant constructor must call a superclass constructor before doing its own work. super( arguments ) may be used to call a specific superclass constructor. If no super( arguments ) or super( ) or this(... ) is written by the programmer, the system will insert super( ) automatically. ( May cause errors. ) this( arguments ) may be used instead of super( ) to call another constructor of this class, which must in turn call super(... ) or this(... ) 7

Interfaces Interfaces work like (pure) abstract classes. An interface specifies a list of capabilities of a class, i.e. a guarantee of certain methods available. Classes may implement multiple interfaces. Interfaces may extend ( multiple ) interfaces. Methods with same names but different signatures are simply overloaded. Methods with same names that differ only by return types or exceptions thrown generate errors. static final variables defined in an Interface are available in any class that implements the Interface, or publicly through the Interface name. Lecture Switches to Textbook at This Point Learning Java by Niemeyer & Leuck, 4 th Edition, O Reilly 8