Inheritance. = way of forming new classes based on existing ones = way to share/reuse codebetween two or more classes

Similar documents
AP Computer Science. Interactions with superclass

Lecture 15: Inheritance II

Building Java Programs

Topic 31 - inheritance

Building Java Programs. Inheritance and Polymorphism

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

CSE 143 Lecture 21. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides created by Marty Stepp

CSE 143 Lecture 22. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides created by Marty Stepp

AP Computer Science. Gridworld, inheritance

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

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

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

ADTs, Interfaces, Inheritance & Polymorphism

Inheritance. Writing classes. Writing more classes. Why are they very similar? Employee class. Secretary class. Readings: 9.1

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Inheritance -- Introduction

CS 112 Introduction to Programming

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

Super-Classes and sub-classes

JAVA MOCK TEST JAVA MOCK TEST II

CS-202 Introduction to Object Oriented Programming

CS 112 Introduction to Programming

Admin. q Admin and recap q Class inheritance

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

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

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

Comp 249 Programming Methodology

Java Object Oriented Design. CSC207 Fall 2014

CS313D: ADVANCED PROGRAMMING LANGUAGE

Basic Object-Oriented Concepts. 5-Oct-17

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CS Programming I: Inheritance

Overriding Variables: Shadowing

ECE 122. Engineering Problem Solving with Java

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Arrays Classes & Methods, Inheritance

INHERITANCE AND EXTENDING CLASSES

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

Programming Abstractions

QUESTIONS FOR AVERAGE BLOOMERS

Chapter 7. Inheritance

Programming Abstractions

BBM 102 Introduction to Programming II Spring Inheritance

ENCAPSULATION AND POLYMORPHISM

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Inheritance. Transitivity

K.Yellaswamy Assistant Professor CMR College of Engineering & Technology

CS1150 Principles of Computer Science Objects and Classes

Implementing AddChoice()

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Chapter 14 Abstract Classes and Interfaces

C++ Important Questions with Answers

CSc 110, Spring 2017 Lecture 37: Critters. Adapted from slides by Marty Stepp and Stuart Reges

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

Classes and Inheritance Extending Classes, Chapter 5.2

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

9/10/2018 Programming Data Structures Inheritance

Accelerating Information Technology Innovation

Chapter 1: Object-Oriented Programming Using C++

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CIS 110: Introduction to computer programming

Unit 4 - Inheritance, Packages & Interfaces

CMSC 132: Object-Oriented Programming II. Inheritance

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

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

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Programming overview

What is Inheritance?

CS 251 Intermediate Programming Inheritance

CS 112 Introduction to Programming. (Spring 2012)

Abstract class & Interface

Inheritance: Definition

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Software and Programming 1

Programming II (CS300)

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

More on Objects in JAVA TM

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

CS 11 python track: lecture 4

CS 106B Lecture 27: Inheritance and Polymorphism in C++

Programming Language Concepts: Lecture 2

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

Universiti Malaysia Perlis

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Lecture 18 CSE11 Fall 2013 Inheritance

Week 5-1: ADT Design

Inheritance and Polymorphism

Transcription:

Inheritance 49

Inheritance Definitions = way of forming new classes based on existing ones = way to share/reuse codebetween two or more classes Terminology superclass: Parent class being inherited from / extended/ specialized. subclass: Child class that inherits behavior from superclass. gets a copy of every field and method from superclass is-a relationship: Each object of the subclass also "is a(n)" object of the superclass and can be treated as one. 50

Inheritance syntax public class NameofSubClass extends NameOfSuperclass { Example By extending Employee, each Lawyer object now: receives a copy of each method / field from Employee automatically can be treated as an Employeeby client code Lawyer can also replace ("override") behavior from Employee. 51

Let s look more into Overriding Definition To write a new version of a method in a subclass that replaces the superclass's version No special syntax required to override a superclass method. Just write a new version of it in the subclass. // overrides getvacationform in Employee class public String getvacationform() { return "pink"; 52

Let s look more into Overriding Definition To write a new version of a method in a subclass that replaces the superclass's version No special syntax required to override a superclass method. Just write a new version of it in the subclass. // overrides getvacationform in Employee class @override public String getvacationform() { return "pink"; https://stackoverflow.com/questions/94361/when-do-you-usejavas-override-annotation-and-why 53

How do subclasses use superclass methods? Subclasses methods may use superclasses methods/constructors: super.method(parameters) super(parameters); // method // constructor public Lawyer(String name) { super(name); // give Lawyers a $5K raise (better) public double getsalary() { double basesalary = super.getsalary(); return basesalary + 5000.00; 54

Rules = How do Subclasses use superclass fields? Subclasses are not allowed to use superclass private fields i.e. Inherited private fields/methods cannot be directly accessed by subclasses aka The subclass has the field, but it can't touch it public class Employee { private double salary;? How can we allow subclasses to access / modify these fields? public void giveraise(double amount) { salary += amount; // error; salary is private 55

Solution = Protected fields/methods protected fields or methods may be seen/called only by: the class itself, its subclasses, other classes in same "package" Syntax protected type name; // field protected type name(type name,, type name) { statement(s); // method Example public class Employee { protected double salary; 56

Inheritance and constructors Problem IF we replace our constructor w/o parameters w/ a constructor that requires parameters in Employee THEN our subclasses do not compile; Lawyer.java:2: cannot find symbol symbol : constructor Employee() location: class Employee ^ Solution IF we write a constructor (that requires parameters) in the superclass THEN must now rewrite constructors for our employee subclasses 57

Let s dig a bit deeper on this Rules = Constructors are not inherited Subclasses don't inherit the Employee(int) constructor. Subclasses receive instead a default constructor that contains: public Lawyer() { super(); // calls Employee() constructor But our Employee(int) replaced the default Employee(). The subclasses' default constructors are now trying to call a nonexistent default Employee constructor. 58

How do we refer to the superclass constructors? Syntax super(parameters); Example public Lawyer(int years) { super(years); // calls Employee c'tor Rules The supercall must be the first statement in the constructor 59