A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Similar documents
CIS 110: Introduction to computer programming

ECE 122. Engineering Problem Solving with Java

CMSC 132: Object-Oriented Programming II. Inheritance

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

Classes and Inheritance Extending Classes, Chapter 5.2

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

Abstract class & Interface

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

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

Darrell Bethea June 6, 2011

OVERRIDING. 7/11/2015 Budditha Hettige 82

Super-Classes and sub-classes

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

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

Inheritance (continued) Inheritance

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Java Object Oriented Design. CSC207 Fall 2014

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

Accelerating Information Technology Innovation

Notes on Chapter Three

1.00 Lecture 13. Inheritance

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

C08: Inheritance and Polymorphism

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

Inheritance and Polymorphism

CS 251 Intermediate Programming Inheritance

COP 3330 Final Exam Review

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

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

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

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

Inheritance and Polymorphism

Inheritance -- Introduction

Programming in C# Inheritance and Polymorphism

What is Inheritance?

Arrays Classes & Methods, Inheritance

CS111: PROGRAMMING LANGUAGE II

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

QUESTIONS FOR AVERAGE BLOOMERS

Example: Count of Points

Week 5-1: ADT Design

Binghamton University. CS-140 Fall Dynamic Types

Exam Duration: 2hrs and 30min Software Design

Review of Object-Oriented Concepts in JAVA

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

CS-202 Introduction to Object Oriented Programming

CS 112 Introduction to Programming

Admin. q Admin and recap q Class inheritance

What are the characteristics of Object Oriented programming language?

Java Programming Unit 4. Abstract Classes, Interfaces, Polymorphism

Java Inheritance. Classes implement the concept of ADT:

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

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

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

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

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

More Relationships Between Classes

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Programming Exercise 14: Inheritance and Polymorphism

Conversions and Overloading : Overloading

Computer Science II (20073) Week 1: Review and Inheritance

First IS-A Relationship: Inheritance

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

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

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

Practice for Chapter 11

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Inheritance Motivation

Java Magistère BFA

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

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

Inheritance. Transitivity

Chapter 9 Inheritance

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CLASS DESIGN. Objectives MODULE 4

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

SSE3052: Embedded Systems Practice

Inheritance and Polymorphism

User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

CS111: PROGRAMMING LANGUAGE II

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

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS260 Intro to Java & Android 03.Java Language Basics

Advanced Placement Computer Science. Inheritance and Polymorphism

Introduction to Inheritance

Chapter 2a Class Relationships

Lecture 4: Extending Classes. Concept

Class, Variable, Constructor, Object, Method Questions

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

Exercise: Singleton 1

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

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

Transcription:

Inheritance A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class. A subclass inherits all of the functionality of the base class and may also define additional functionality specific to the child class. A subclass has an "is-a" relationship with the parent class. A parent class can have many subclasses, but a subclass can only extend one parent class. However, a subclass can implement many interfaces. The data members in the parent class are usually defined with the protected visibility modifier, instead of private, to ensure that the data is visible to its subclasses. The protected modifier exposes the data members to other classes in the package. Note: If the keyword extends is not used to define a class, that class implicitly extends the Object class. Class A below extends the Object class. Abstract Class public class A { protected int x; public class B extends A { private int y; An abstract class is a class that has 1 or more abstract methods. public abstract class A { protected int x, y; public abstract int methoda(); public int larger(a other){ if(methoda() > other.methoda()) return x; return y;

Interface An interface is similar to a fully abstract class. It consists of public abstract methods and public static final fields, only. A class that implements an interface must provide definitions for all of the abstract methods in the interface. Otherwise, the class must be declared as abstract. The visibility of the inherited method from the interface must be public. While a class can extend only one other class, it may implement many interfaces. Just like a class, the IS-A relationship holds. The instanceof operator can be used to determine if a reference is typecompatible with an interface. When a class implements an interface method, it must implement the exact signature; otherwise, it inherits the abstract version from the interface, and the method written is an overloaded version of the method. A class may not implement two interfaces that contain a method with the same signature and incompatible return types, since it would be impossible to provide both methods in one class. If a class fails to implement all of the methods inherited from an interface, it must be declared abstract. Interfaces can extend other interfaces (including multiple interfaces). public interface Comparable { int compareto(object o); public class BlackJackCard implements Comparable { private int rank, suit; public int compareto(object o) { int val = -1; if(o instanceof BlackJackCard) val = rank - ((BlackJackCard)(o)).rank; return val;

Polymorphism Literally means many forms. An object variable can take on different forms. A superclass reference variable can refer to any of its descendants. Dynamic/Late Binding: At run-time, Java determines which version of an inherited method is called by evaluating the contents of the object, not the reference. Staff staff = new Staff("Mary Smith", "N00975310","C3078",49,35); Employee emp = staff; System.out.println(emp.pay()); The following is an invalid statement because Employee is an abstract class. Employee employee = new Employee("Mary Smith", "N00975310", "C3078"); Casting: If a method exists in the subclass, but not the superclass, a reference of the superclass type cannot call that method. Even though p refers to a Staff type, p.pay() is undefined. Person p = staff; System.out.println(((Staff)(p)).pay());

public class InheritanceApp { public static void main(string[] args) { Faculty faculty = new Faculty("Dave Watson", "N00543210","B3038",2000); Staff staff = new Staff("Mary Smith", "N00975310","C3078",49,35); Student student = new Student("John Doe", "N00123456", 2.54); System.out.println(faculty); System.out.println(student); System.out.println(staff); // A polymorphic reference Person[] person = new Person[] {faculty, staff, student; // Late or dynamic binding for(int i=0; i < person.length; i++) System.out.println(person[i]); Output from the program above: Name: Dave Watson ID: N00543210 Email Address: Dave.Watson@qc.cuny.edu Weekly Salary: 2000.0 Name: John Doe ID: N00123456 Email Address: John.Doe@qc.cuny.edu Name: Mary Smith ID: N00975310 Email Address: Mary.Smith@qc.cuny.edu Weekly Salary: 1715.0 Name: Dave Watson ID: N00543210 Email Address: Dave.Watson@qc.cuny.edu Weekly Salary: 2000.0 Name: Mary Smith ID: N00975310 Email Address: Mary.Smith@qc.cuny.edu Weekly Salary: 1715.0 Name: John Doe ID: N00123456 Email Address: John.Doe@qc.cuny.edu

class Person { protected String name, id; public Person(String n, String i){ name = n; id = i; public String tostring(){ String str = ""; str += "\nname: " + name + "\nid: " + id; str += "\nemail Address: " + email(); return str; public String email(){ return name + "@qc.cuny.edu"; /*****************************/ abstract class Employee extends Person { protected String office; public Employee(String n, String i, String o){ super(n,i); office = o; public abstract double pay(); public String tostring(){ String str = super.tostring(); str += "\nweekly Salary: " + pay(); return str; /*****************************/ class Student extends Person { protected double gpa; public Student(String n, String i, double g){ super(n, i); gpa = g;

class Faculty extends Employee { private double salary = 1500; public Faculty(String n, String i, String o, double s){ super(n, i, o); salary = s; public double pay(){ return salary; /*****************************/ class Staff extends Employee { private double rate, hours; public Staff(String n, String i, String o, double r, double h){ super(n, i, o); rate = r; hours = h; public double pay() { return hours * rate;