Road Map Inheritance Class hierarchy Overriding methods Constructors

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Chapter 1 Inheritance University Of Ha’il.
OOP: Inheritance By: Lamiaa Said.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance.
Advanced Programming in Java
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Road Map Introduction to object oriented programming. Classes
Advanced Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Computer Programming with JAVA
Java – Inheritance.
Java Programming, Second Edition
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 7 Inheritance.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Computer Science I Inheritance Professor Evan Korth New York University

Road Map Inheritance Class hierarchy Overriding methods Constructors Modifier: protected Keyword: super Evan Korth New York University

Introduction to Inheritance Often programmers find themselves designing classes with many similarities. How can we take advantage of these similarities to reduce the amount of code we write? How can we take advantage of these similarities to write code which is less likely to contain errors? How can we take advantage of existing code when we want to design something similar to the existing code? Evan Korth New York University

Introduction to Inheritance Inheritance is an object oriented construct which promotes code reuse by allowing you to create a new class using an existing classes as a “starting point”. You simply state which class you want to use as a base in order to inherit its members (fields and methods). You can add or modify its members once you have inherited them. Terminology Superclass (parent class, base class) The original class which has its members inherited Typically, this is the more general class Subclass (child class, derived class) The class which inherits members from the superclass In this class, we can specialize the general class by modifying and adding functionality. Evan Korth New York University

Inheritance Abstraction “is-a” vs. “has-a” Focus on commonalities among objects in system “is-a” vs. “has-a” “is-a” Inheritance subclass object treated as superclass object Example: Car is a vehicle Vehicle properties/behaviors also car properties/behaviors “has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel

9.2 Superclasses and Subclasses Object of one class “is an” object of another class Example: Rectangle is quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral: superclass Rectangle: subclass Superclass typically represents larger set of objects than subclasses Example: superclass: Vehicle Cars, trucks, boats, bicycles, … subclass: Car Smaller, more-specific subset of vehicles

9.2 Superclasses and Subclasses (Cont.) Inheritance examples

9.2 Superclasses and Subclasses (Cont.) Inheritance hierarchy Inheritance relationships: tree-like hierarchy structure Each class becomes superclass Supply data/behaviors to other classes AND / OR subclass Inherit data/behaviors from other classes

Fig. 9.2 Inheritance hierarchy for university CommunityMembers. Employee Student Staff Faculty Administrator Teacher Alumnus Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

ThreeDimensionalShape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron Fig. 9.3 Inheritance hierarchy for Shapes.

Inheritance in Java The keyword extends is used in Java for inheritance. A class that extends another class is said to be a subclass. It is also known as a child class or a derived class. The class it extends is called a superclass. Sometimes the superclass is called a base class or a parent class. A subclass can also modify its method members. This is referred to as overriding. A subclass inherits the members of it’s superclass. Syntax: public class Student extends CommunityMember Evan Korth New York University

Overriding methods Often a subclass does not want to have the same exact method implementations as it’s superclass. In this case, the subclass can override the methods that it wants to change. To override a method, you must re-declare the method in the subclass. The new method must have the exact same signature as the superclass’ method you are overriding. Only non-private methods can be overridden because private methods are not visible in the subclass. Note: You cannot override static methods. Note: You cannot override fields. In the above two cases, when you attempt to override them, you are really hiding them. Evan Korth New York University

Overload vs Override Overloading a method refers to having two methods which share the same name but have different signatures. Overriding a method refers to having a new implementation of a method with the same signature in a subclass. Evan Korth New York University

Keyword super The keyword super can be used in a subclass to refer to members of the subclass’ superclass. It is used in two ways: To invoke a constructor of the superclass (more about this on the next slide). For example (note: the keyword new is not used): super (); super (params); To call a superclass’ method (it is only necessary to do so when you override (more on overriding soon too) the method). For example: super.superclassMethodName(); super.superclassMethodName(params); Evan Korth New York University

Inheritance and constructors Constructors do not get inherited to a subclass. Instead, the subclass’ constructor automatically calls it’s superclass’ zero parameter constructor before it executes any of the code in it’s own constructor. If you do not want to use the parent’s zero parameter constructor, you must explicitly invoke one of the superclass’ other constructors with the keyword super. If you do this, the call to the superclass’ constructor must be the first line of code in the subclass’ constructor. e.g: Student (String name) { super (?params?); … If the superclass has no zero parameter constructor, the first line of any subclass constructor MUST explicitly invoke another superclass constructor. Evan Korth New York University

Indirect Inheritance It is possible in Java to have an arbitrarily long line of ancestors (i.e. many superclasses on your way to Object) The members you inherit from classes above your superclass are said to be indirectly inherited. It does not matter where a class’ members come from – a client program always uses similar syntax to access the fields and methods (whether you define it in a class, inherit it directly from your superclass or indirectly from another class in the hierarchy). Evan Korth New York University