Lecture 18 CSE11 Fall 2013 Inheritance

Similar documents
Inheritance -- Introduction

Inheritance and Polymorphism

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

Java Object Oriented Design. CSC207 Fall 2014

COMP 110/L Lecture 19. Kyle Dewey

What is Inheritance?

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance and Polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS-202 Introduction to Object Oriented Programming

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

Inheritance. Transitivity

INHERITANCE. Spring 2019

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

PROGRAMMING LANGUAGE 2

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

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

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

Programming in C# Inheritance and Polymorphism

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

ECE 122. Engineering Problem Solving with Java

CSCE3193: Programming Paradigms

Chapter 5 Object-Oriented Programming

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

CS313D: ADVANCED PROGRAMMING LANGUAGE

Practice for Chapter 11

Polymorphism and Inheritance

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

25. Generic Programming

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

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

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

INHERITANCE AND EXTENDING CLASSES

Inheritance and Interfaces

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

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

Overriding Variables: Shadowing

ITI Introduction to Computing II

CS111: PROGRAMMING LANGUAGE II

ITI Introduction to Computing II

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

CS111: PROGRAMMING LANGUAGE II

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

Programming II (CS300)

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

CS111: PROGRAMMING LANGUAGE II

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

CS1150 Principles of Computer Science Objects and Classes

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

9/10/2018 Programming Data Structures Inheritance

CS 251 Intermediate Programming Inheritance

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

More on Objects in JAVA TM

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

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

Chapter 7. Inheritance

Inheritance, Polymorphism, and Interfaces

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

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

Programming overview

Arrays Classes & Methods, Inheritance

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

1.00 Lecture 13. Inheritance

8. Polymorphism and Inheritance

Java Fundamentals (II)

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

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Classes and Inheritance Extending Classes, Chapter 5.2

CS Programming I: Inheritance

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Check out Polymorphism from SVN. Object & Polymorphism

Computer Science 210: Data Structures

Inheritance and Polymorphism

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

BBM 102 Introduction to Programming II Spring Inheritance

Everything is an object. Almost, but all objects are of type Object!

C08: Inheritance and Polymorphism

Object-Oriented Concepts

CMSC 132: Object-Oriented Programming II. Inheritance

Lecture 4: Extending Classes. Concept

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

C++ Important Questions with Answers

Introduction to Inheritance

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

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

Comp 249 Programming Methodology

Inheritance and Encapsulation. Amit Gupta

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

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

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

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CMSC 132: Object-Oriented Programming II

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

Inheritance (Outsource: )

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Transcription:

Lecture 18 CSE11 Fall 2013 Inheritance

What is Inheritance? Inheritance allows a software developer to derive a new class from an existing one write code once, use many times (code reuse) Specialization extends is the java keyword that indicates inheritance The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass. As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined for the parent class

Inheritance Hierarchy Person UCSD_Person Student Instructor Undergrad Grad TA Professor?? Can Only have one Parent! Tutor

isa Defines an inheritance relationship Examples: UCSD_Person isa person Instructor isa UCSD_Person Student isa UCSD_Person Undergrad isa Student TA isa Instructor Transitive: TA isa UCSD_Person Undergrad isa Person

What do you get when you inherit (extend a class) all methods and variables But, if a method/variable is private the subclass cannot access the method/variable Private means private to the class in which the method/variable is defined constructor(s) of your parent It's recursive, you get method/variables/constructors of your parent, grandparent, great-grandparent...

the Object class Every java class is descended from the Object class. Object defines a few interesting methods (and hence ALL classes have these methods) getclass() - returns the runtime class tostring() - returns a String Representation of the class equals() - method to determine if two objects are equal to each other (Note the String class defines the equals() method to be a character by character comparison of two string objects)

Constructors The constructor for every parent class is called whenever you do a new If your code does not supply it, the noparameter constructor of you parent is implicitly inserted by the compiler as the first line of your constructor Your constructor can explicitly call super(....) as the first line of your subclass constructor. If it does. it must be the first statement.

Hierarchy Revisited Class 0 (Object) private variables methods private variables methods Class 1 Class 2 private variables and methods inherited variables and methods Constructor of 2 Calls Constructor of 1. Constructor 1 Calls Constructor of 0. This is so each layer of the hierarchy can initialize all private variables

Dynamic Method Invocation Java always uses the method defined closest to the class when the instance was created Suppose ClassC extends ClassA ClassC is a subclass, ClassA is the superclass Both classes define methodx() Now suppose you declare ClassC myinstance = new ClassC(); Which methodx() code is executed in MyInstance.methodX()? Now Declare ClassA referasa = myinstance; Which method is invoked via referasa.methodx();

protected Private variables/methods are private to the class. They CANNOT be seen by any subclasses Public variables/methods are available to all classes (including subclasses) protected variables/methods are seen by subclasses, but not by external classes Declare a method/variable as public, private or protected canvas is a protected variable of the WindowController Class This is why you can use it without declaring it

Overriding Method Defintions A subclass can redefine a method with the identical signature of its superclass. There are times when you want to invoke the method of your super class when (re)defining in your subclass use the super.method() to invoke method() of your Superclass

final Have applied final to variables to make them into constants You can apply final to methods to indicate that they cannot be overridden by subclasses You can apply final to classes to indicate that they cannot be extended e.g. public final class Math

abstract abstract methods are methods with no body They look a lot like interfaces To be useful, a subclass must provide an implementation for abstract method If a class defines an abstract method, the class must be defined as abstract Purpose: define a hierarchy of methods/capability (outline of functionality) The AWT has many examples of abstract http://docs.oracle.com/javase/6/docs/api/java/awt/toolkit.html