Inheritance. Transitivity

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

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

Java Fundamentals (II)

Inheritance and Polymorphism

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

CS 251 Intermediate Programming Inheritance

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Chapter 10 Classes Continued. Fundamentals of Java

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

INHERITANCE. Spring 2019

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

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance (Part 5) Odds and ends

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

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

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

Inheritance and Substitution (Budd chapter 8, 10)

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lecture 18 CSE11 Fall 2013 Inheritance

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

QUESTIONS FOR AVERAGE BLOOMERS

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

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

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

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Overriding Variables: Shadowing

ECE 122. Engineering Problem Solving with Java

Self-review Questions

Declarations and Access Control SCJP tips

Instance Members and Static Members

Introduction to Programming Using Java (98-388)

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

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

COMP 110/L Lecture 20. Kyle Dewey

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

Chapter 14 Abstract Classes and Interfaces

What is Inheritance?

CS-202 Introduction to Object Oriented Programming

ITI Introduction to Computing II

Java Object Oriented Design. CSC207 Fall 2014

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

COMP 110/L Lecture 19. Kyle Dewey

Object Orientated Programming Details COMP360

Introduction to Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

ITI Introduction to Computing II

Java Magistère BFA

Class, Variable, Constructor, Object, Method Questions

CS Programming I: Inheritance

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

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Rules and syntax for inheritance. The boring stuff

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

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

The class Object. Lecture CS1122 Summer 2008

Topic 5: Abstract Classes & Interfaces

First IS-A Relationship: Inheritance

Inheritance (Part 2) Notes Chapter 6

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Inheritance and object compatibility

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

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

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

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

More on Objects in JAVA TM

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

CT 229 Object-Oriented Programming Continued

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

Inheritance (an intuitive description)

Example: Count of Points

Super-Classes and sub-classes

Java Classes, Inheritance, and Interfaces

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

More Relationships Between Classes

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

UCLA PIC 20A Java Programming

Inheritance, and Polymorphism.

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

Inheritance. The Java Platform Class Hierarchy

Advanced Placement Computer Science. Inheritance and Polymorphism

CS1150 Principles of Computer Science Objects and Classes

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

C++ Important Questions with Answers

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

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

Arrays Classes & Methods, Inheritance

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Building custom components IAT351

PROGRAMMING LANGUAGE 2

Polymorphism and Inheritance

Overriding methods. Changing what we have inherited from e.g. Object

Transcription:

Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated with a superclass In programming languages, inheritance means that the behavior and data associated with subclasses are always an extension of the properties associated with the parent class A child is a more specialized form of the parent 9/10/2003 Inheritance 1 Transitivity Inheritance is always transitive A class can inherit features from superclasses many levels away If class dog is a subclass of class mammal, and class mammal is a subclass of animal, then dog will inherit attributes from both mammal and animal Multiple inheritance occurs when a subclass has more than one superclass Java does not support multiple inheritance, but other OO languages do (C++, Eiffel, ) 9/10/2003 Inheritance 2 1

Since Substitutability Instances of a subclass contain all the state and behavior associated with the superclass This means An instance of a subclass can mimic the behavior of the superclass and should be indistinguishable from an instance of the superclass So It is possible to substitute instances of the subclass for the superclass in any situation with no observable effect 9/10/2003 Inheritance 3 Forms of Inheritance Form of Inheritance Specialization Specification Generalization Extension Limitation Combination Description The subclass is a specialized form of the superclass but satisfies the specifications of the parent class in all relevant aspects. The superclass defines behavior that is implemented in the subclass but not in the superclass. The subclass modifies or overrides some of the methods of the superclass. The subclass adds new functionality to the parent class, but does not change any inherited behavior. The subclass restricts the use of some of the behavior inherited from the superclass. The subclass inherits features from more than one superclass (i.e. multiple inheritance) 9/10/2003 Inheritance 4 2

Syntax A subclass inherits from a superclass using the extends keyword class subclassname extends superclassname { variable and method declarations Inheritance is applicable to top-level classes, nested top-level classes, member classes, local classes and anonymous classes 9/10/2003 Inheritance 5 Inheritance A class can inherit from any class that is not final. Objects of the subclass contain all the instance variables and methods declared by the superclass. The accessibility rules are still enforced which means a subclass cannot access the private parts of the superclass. Subclassing can be repeated as many times as desired. A class can have only one superclass, but may have many subclasses. 9/10/2003 Inheritance 6 3

Scope Rules Inheritance increases the number of scopes that need to be searched (both static and instance declarations are searched). Check the local scope and any local scopes. Check the class scope. Check each superclass scope in turn up to the top of the inheritance chain. If variables with the same identifier are declared in several scopes, the first one found is used. 9/10/2003 Inheritance 7 Method Overloading Methods can be overloaded, meaning that two or methods in the same class can have the same name provided they have different parameter lists. The return type for all overloaded methods must be the same. Operator overloading is not supported in java. 9/10/2003 Inheritance 8 4

Method Overriding A subclass can override an inherited method by providing a new method declaration that has the same name, the same number and types of parameters and the same result type as the one inherited. Method overriding relies on dynamic binding, so the type of the object determines which method gets called. 9/10/2003 Inheritance 9 Abstract Classes An abstract class is a place holder for declaring shared methods and variables for use by subclasses. An abstract class cannot have instance objects and so exists as a class that other classes can inherit from. A concrete class is a class that is not abstract. 9/10/2003 Inheritance 10 5

Abstract Methods A method can be declared abstract so that it must be overridden by subclasses. An abstract class does not have a method body; The declaration ends with a semi-colon not a compound statement. A class declaring one or more abstract methods must be declared as an abstract class. Private and static methods cannot be abstract. 9/10/2003 Inheritance 11 Stack abstract class Stack { protected int count = 0; public abstract void push( Object o ); public abstract void pop(); public abstract Object top(); public abstract boolean isfull(); public boolean isempty() { return count==0; 9/10/2003 Inheritance 12 6

ArrayStack public class ArrayStack extends Stack { private Object data[]; private tos = -1; public ArrayStack() { data = new Object[ 100 ]; public void push( Object o ) { if (!isfull() ) { tos++; data[ tos ] = o; count++; public void pop() { if (!isempty() ) { tos--; count--; public Object top() { return data.lastelement(); public boolean isfull() { return tos == ( data.length 1); 9/10/2003 Inheritance 13 Final Methods A final instance method cannot be overridden (but can still be overloaded). A final static method cannot be re-declared in a sublcass. Final methods prevent a method that has the same name and parameter types from being declared in a subclass. This takes into account both static and instance variables. 9/10/2003 Inheritance 14 7

Constructors and Inheritance The guarantee of proper initialization must be maintained in the presence of inheritance. Java forces the constructors for each superclass to be called and provides syntax for explicitly controlling which constructors are called. The keyword super can be used to explicitly call a superclass constructor. super ( argumentlist ) ; super must be the first statement in a constructor. 9/10/2003 Inheritance 15 Methods Inherited From Class Object Class Object declares the following methods that can be overwritten: public boolean equals( Object obj ); public String tostring(); public final native int hashcode() ; protective native Object clone(); protected void finalize(); public final Class getclass() 9/10/2003 Inheritance 16 8

Interfaces An interface declaration allows the specification of a reference type without providing an implementation. A type can conform to another type if it specifies at least the same set of methods as the other type (and possibly more). The two types do not have to be related by inheritance which gives more freedom as to which types may conform to other types. 9/10/2003 Inheritance 17 Syntax An interface is declared as shown below: interfacemodifier interface identifier { interfacemethoddeclarations; interfacevariabledeclarations; The optional modifier allows an interface to be declared public. Any variables declared are implicitly constants and are also static. 9/10/2003 Inheritance 18 9

Implements The implements keyword allows a class to implement (or conform to) one or more interfaces. A class can implement any number of interfaces (and also extend a class at the same time). Any variables defined in the interface become static variables of the class. A method declared in a public interface must be public in an implementing class. 9/10/2003 Inheritance 19 Inheritance and Association Inheritance actually serves two distinct purposes. A mechanism for sharing implementation by making one class an extension of another. A mechanism for sharing a public interface such that an object of one class can safely be substituted for an object of another class. Is inheritance the only way to share code? An alternative is to put shared methods and variables into another class and gain access via an instance variable referring to an object of that class. 9/10/2003 Inheritance 20 10

Inheritance and Association How do you decide which is approach to use in a given situation? You have to think carefully about why the classes need to be related If a class simply needs to use the implementation of another class, but does not need the same public interface then association should be used If the decision is made to use inheritance, then both the implementation and the public interface of the proposed superclass as needed If a class Y is-a or is-a-kind-of class X, then inheritance is probably the right way to go 9/10/2003 Inheritance 21 11