INHERITANCE AND EXTENDING CLASSES

Similar documents
Java Object Oriented Design. CSC207 Fall 2014

ECE 122. Engineering Problem Solving with Java

Programming overview

Lecture 18 CSE11 Fall 2013 Inheritance

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

Arrays Classes & Methods, Inheritance

Making New instances of Classes

What is Inheritance?

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

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

Building custom components IAT351

INHERITANCE. Spring 2019

Inheritance Motivation

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Introduction to Computer Science II CS S-10 Inheritance

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

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

Chapter 10 Classes Continued. Fundamentals of Java

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Practice for Chapter 11

Programming II (CS300)

More on Objects in JAVA TM

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

Class, Variable, Constructor, Object, Method Questions

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

C++ Important Questions with Answers

Inheritance -- Introduction

COMP 110/L Lecture 19. Kyle Dewey

One of these "compartments" is more correctly referred to as an element of the array

CS111: PROGRAMMING LANGUAGE II

Chapter 5. Inheritance

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

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

Comp 249 Programming Methodology

QUESTIONS FOR AVERAGE BLOOMERS

COMP 250 Fall inheritance Nov. 17, 2017

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

JAVA MOCK TEST JAVA MOCK TEST II

Inheritance. Transitivity

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Basics of Object Oriented Programming. Visit for more.

PROGRAMMING LANGUAGE 2

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

Chapter 14 Abstract Classes and Interfaces

ITI Introduction to Computing II

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

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

Inheritance CSC 123 Fall 2018 Howard Rosenthal

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

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

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

Self-review Questions

Inheritance (Outsource: )

JAVA Programming Language Homework I - OO concept

ITI Introduction to Computing II

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

Week 11: Class Design

Overriding Variables: Shadowing

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

Rules and syntax for inheritance. The boring stuff

Object Oriented Programming: Based on slides from Skrien Chapter 2

Creating Java Programs with Greenfoot

Chapter 3: Inheritance and Polymorphism

Inheritance (Part 5) Odds and ends

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

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

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

CS 251 Intermediate Programming Inheritance

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

More on Inheritance. Interfaces & Abstract Classes

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

8. Polymorphism and Inheritance

CS111: PROGRAMMING LANGUAGE II

Programming II (CS300)

C08: Inheritance and Polymorphism

What are the characteristics of Object Oriented programming language?

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

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

Inheritance, and Polymorphism.

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

Java Fundamentals (II)

Interfaces, Mixins, & Multiple Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

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

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

Full file at Chapter 2 - Inheritance and Exception Handling

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

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object-Oriented Programming More Inheritance

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

Transcription:

INHERITANCE AND EXTENDING CLASSES Java programmers often take advantage of a feature of object-oriented programming called inheritance, which allows programmers to make one class an extension of another existing class. A class that is based on or extended from another class is said to inherit the other class. The class that is extended is called the parent class or the superclass. The class that extends another class is called the child class or the subclass. A class can have many levels of inheritance. For example, consider the following hierarchy: Automobile Honda Accord Civic In the above example, the Accord and Civic classes inherit the Honda class, which inherits the Automobile class. The Automobile class is the superclass of Honda. Honda is the subclass of Automobile and the superclass of Accord and Civic. The Accord and Civic classes are subclasses of Honda. If we were writing software that represented vehicles, you would start by creating a class called Automobile that would describe the features that are common to all types of vehicles (i.e. wheels, a driver, the ability to carry passengers, etc.) A Honda is a type of vehicle that further refines the Automobile class. The Honda class would inherit the Vehicle class and its attributes (i.e. wheels, a driver, the ability to carry passengers, etc.) but would have additional features that differentiate it from other types of vehicles (such as Toyotas, Nissans, Volkswagens, etc.). IMPLEMENTING A SUBCLASS A class that extends another class includes the keyword extends in the class declaration and takes the following form: public class ClassName extends Superclass { Let s say, for example, we have a class named Ball that defines a basic ball and we wanted to create a subclass named BouncingBall that bounces. We would create that subclass as follows: Inheritance and Extending Classes Page 1 of 5

public class BouncingBall extends Ball { public void bounce() { In the above example, the BouncingBall class inherits all the methods and fields of the Ball class. So, if the Ball class has fields named size and diameter, the BouncingBall class has those fields too. Also, if the Ball class has a method named spin(), the BouncingBall class inherits that method as well. Some important things you need to know about extending classes: A subclass inherits all the fields and methods from the superclass. Constructors from the superclass, however, are not inherited. A subclass cannot access methods or fields that are declared as private in the superclass. You can override a method in a subclass by declaring a new method with the same signature. You can add additional methods or fields, private or protected, to a subclass. OVERRIDING METHODS If a subclass declares a method that has the same signature as a public method of the superclass, the subclass version of the method overrides the superclass version of the method. This allows the programmer to modify the behaviour of the superclass to suit the needs of the subclass. Let s say, for example, we have a superclass named Game that has a method named startgame(). The superclass, which doesn t represent any particular game, implements the following method: public class Game { public void startgame() { JOptionPane.showMessageDialog(null, Ready, set, go!, Game, JOptionPane.INFORMATION_MESSAGE); We can declare a subclass called Poker that extends the Game class and overrides the startgame() so that it outputs a different message: public class Poker extends Game { public void startgame() { Inheritance and Extending Classes Page 2 of 5

JOptionPane.showMessageDialog(null, Shuffle up and deal!, Poker, JOptionPane.INFORMATION_MESSAGE); In order to override a method, the following conditions must be met: The class must extend the class that defines the method you want to override. The method must be declared in the superclass with public access. You can t override a private method. The method in the subclass must have the same signature as the method in the superclass (i.e. the name of the method and the parameter types must be the same) USING SUPER IN YOUR SUBCLASSES If you want to refer to a field or method that belongs to a superclass, you can do so by using the super keyword. It works similar to the keyword this, but refers to the instance of a superclass instead of the instance of the current class. In the following example, the outputmessage() method in the AMGreeting class calls the outputmessage() method of its superclass object and then outputs its own message: public class Greeting { public void outputmessage() { System.out.print( Hello! ); public class AMGreeting extends Greeting { public void outputmessage() { super.outputmessage(); System.out.println( Have a great day! ); INHERITANCE AND CONSTRUCTORS When you create an instance of a subclass, the default constructor of the superclass is automatically called before it executes the constructor of the subclass. In the following example, Hello! will be outputted followed by Have a great day! when you create an instance of the AMGreeting class: Inheritance and Extending Classes Page 3 of 5

public class Greeting { public Greeting() { System.out.print( Hello! ); public class AMGreeting extends Greeting { System.out.println( Have a great day! ); You can explicitly call a superclass constructor from a subclass by using the super keyword. Since the default constructor is automatically called, the only time you would want to do this is in the case of a constructor that takes a parameter that you want to set. In the following example, I have called the constructor of the Ball class so that I can specify the diameter of the ball: public class Ball { private double diameter; public Ball(double d) { diameter = d; public class PongBall extends Ball { public PongBall() { super(30); The following are some rules you ll need to keep in mind when working with superclass constructors: If you use super to call the superclass constructor, you must do so in the very first statement in the constructor If you don t explicitly call super, the compiler inserts a call to the default constructor of the superclass. But if the superclass doesn t have a default constructor, it results in a compiletime error. Inheritance and Extending Classes Page 4 of 5

If the superclass is itself a subclass, the constructor for its superclass is called in the same way. Inheritance and Extending Classes Page 5 of 5