Inheritance. Chapter 7. Chapter 7 1

Similar documents
8. Polymorphism and Inheritance

Inheritance, Polymorphism, and Interfaces

Polymorphism and Inheritance

CS 251 Intermediate Programming Inheritance

Inheritance -- Introduction

CSCE3193: Programming Paradigms

ECE 122. Engineering Problem Solving with Java

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

Programming in C# Inheritance and Polymorphism

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Polymorphism

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

What is Inheritance?

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

CS-202 Introduction to Object Oriented Programming

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Inheritance and Polymorphism

INHERITANCE. Spring 2019

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

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

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

QUESTIONS FOR AVERAGE BLOOMERS

Inheritance and Polymorphism

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

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

Chapter 4 Defining Classes I

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

CS112 Lecture: Inheritance and Polymorphism

Java: introduction to object-oriented features

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

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

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

Inheritance (Deitel chapter 9)

Inheritance (continued) Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

First IS-A Relationship: Inheritance

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

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

C++ Important Questions with Answers

CS180 Recitation. More about Objects and Methods

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Programming Exercise 14: Inheritance and Polymorphism

Final Exam CS 251, Intermediate Programming December 13, 2017

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

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

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Java Programming Lecture 7

Defining Classes and Methods

Darrell Bethea June 6, 2011

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

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Lecture 18 CSE11 Fall 2013 Inheritance

Practice for Chapter 11

More Relationships Between Classes

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

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

Window Interfaces Using Swing Objects

More About Objects and Methods

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

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

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

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II. Inheritance

Inheritance and Polymorphism

Data Structures and Other Objects Using C++

ITI Introduction to Computing II

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

Java Fundamentals (II)

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

Window Interfaces Using Swing Objects

Chapter 7. Inheritance

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Inheritance, and Polymorphism.

OVERRIDING. 7/11/2015 Budditha Hettige 82

Chapter 6 Introduction to Defining Classes

C08: Inheritance and Polymorphism

CLASS DESIGN. Objectives MODULE 4

Classes and Inheritance Extending Classes, Chapter 5.2

Chapter 6 Reflection

ITI Introduction to Computing II

Data Structures (list, dictionary, tuples, sets, strings)

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CS111: PROGRAMMING LANGUAGE II

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Building custom components IAT351

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

Object Orientated Programming Details COMP360

Chapter 14 Abstract Classes and Interfaces

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

Defining Classes and Methods

Transcription:

Inheritance Chapter 7 Chapter 7 1

Introduction to Inheritance Inheritance allows us to define a general class and then define more specialized classes simply by adding new details to the more general class definition. A more specialized class inherits the properties of the more general class, so that only new features need to be programmed. Chapter 7 5

Introduction to Inheritance, cont. example General class Vehicle might have instance variables for weight and maximum occupancy. More specialized class Automobile might add instance variables for wheels, engine size, and license plate number. General class Vehicle might also be used to define more specialized classes Boat and Airplane. Chapter 7 6

Programming Example: A Base Class class Person Chapter 7 7

Derived Classes Consider a college record-keeping system with records about students, faculty and staff. Chapter 7 8

Derived Classes, cont. Chapter 7 9

Derived Classes, cont. Even though your program may not need any Person or Employee objects, these classes can be useful for consolidating and representing features common to all subclasses. For example, all students, faculty, and staff have names, and these names may need to be initialized, changed, retrieved, or printed. Chapter 7 10

Derived Classes, cont. class Student is a derived class of class Person and class Person is called the base class. Chapter 7 11

Derived Classes, cont. public class Student extends Person Chapter 7 12

Derived Classes, cont. When you define a derived class, you declare only the added instance variables and you define only the added and overridden methods. The variables and methods of the parent class which are not declared private are inherited automatically. Chapter 7 14

Derived Classes, cont. class InheritanceDemo Chapter 7 15

Overriding Method Definitions Notice that class Student has a method writeoutput with no parameters, and class Person also has a method writeoutput with no parameters, that class Student inherits. When a derived class defines a method with the same name and the same number and types of parameters as a method in the base class, the method in the derived class overrides the method in the base class. Chapter 7 17

Overriding Method Definitions, cont. When overriding a method, you can change the method definition to anything you wish, but you cannot change the method s heading or the method s return type. Chapter 7 18

Overriding vs. Overloading When you override a method, the new method definition in the derived class has the same name and the same number of types of parameters as the method definition in the base class. When the name is the same, but the number or types of the parameters differs, whether in the base class or in the derived class, the method is overloaded in the derived class Chapter 7 19

Overriding vs. Overloading, cont. example: public String getname(string title) in class Student and public String getname() in class Person overload method getname since the two methods have different parameter lists. Both methods are available in class Student. Chapter 7 20

Private Instance Variables in the Base Class Private instance variables inherited from a base class cannot be accessed directly. Instead, they must be accessed using a method that is not declared private. While this may seem inconvenient, it provides an important mechanism for controlling access and changes to instance variables in the base class. Chapter 7 23

Private Methods in the Base Class Like private instance variables, private methods inherited from a base class cannot be accessed directly. Instead, they, too, must be accessed using a method that is not declared private. This, too, provides an important mechanism for controlling access to methods in the base class. Chapter 7 24

Private Methods in the Base Class Since private methods typically serve as helping methods, their use always is limited to the class in which they are defined. Chapter 7 25

Constructors in Derived Classes A base class has its own constructors. Their purpose typically is to initialize the instance variables declared in the base class. A derived class has its own constructors. Their purpose typically is to call a constructor in the base class, and then to initialize the instance variables declared in the derived class. Chapter 7 31

Constructors in Derived Classes, cont. To call a constructor in the base class, use super(values_for_instance_variables _Declared_in_the_Base_Class); example super(initialname); not Person(initialName); //ILLEGAL Chapter 7 32

Using super The call to the constructor in the base class (using super) must be the first action taken in the constructor of a derived class. When no call to the constructor in the base class is included, Java automatically includes a call to the default constructor in the base class. Chapter 7 33

Using super, cont. equivalent definitions: public Student() { super(); studentnumber= 0; } and public Student() { studentnumber= 0; } Chapter 7 34

The this Method Within the definition of one constructor, it can be appropriate to call another constructor in the same class. The keyword this is used to call another constructor in the same class. example this(initialname, 0) Chapter 7 35

The this Method, cont. Any use of this must be the first action in the constructor definition. Thus, a constructor definition cannot contain a call using super and a call using this. To use both super and this, include a call using this in one constructor and a call using super in the constructor called using this. Chapter 7 36

Calling an Overridden Method super can be used to call a method in the base class that has been overridden in the derived class. example super.writeoutput(); However, you cannot repeat the use of super to invoke a method in some ancestor class other than the immediate base (parent) class. Chapter 7 37

An Object Can Have More than One Type If class Undergraduate is derived from class Student and class Student is derived from class Person, then every object of class Undergraduate is also an object of class Student and an object of class Person. A reference to an object of class Undergraduate can be substituted for a reference to an object of class Student or a reference to an object of class Person. Chapter 7 45

An Object Can Have More than One Type, cont. However, a reference to an object of class person cannot be substituted for a reference to an object of class Student or an object of class Undergraduate. A reference to an object of an ancestor cannot be substituted for a reference to an object of a derived class. Chapter 7 47

The Class Object In Java, every class descends from (and inherits features from) the Object class. Therefore, every object of every class is of type Object. Unless a class is declared explicitly to be a descendant of some other class, it is an immediate descendant of the class Object. Chapter 7 50

The Class Object, cont. An object of any class can substituted when a parameter of type Object is expected. Every class inherits some methods from the class Object: equals() tostring() but usually these methods are overridden by the derived class or by an intermediate ancestor class. Chapter 7 51

Method tostring Inherited method tostring takes no arguments. Typically, method tostring is coded to produce and return a string which contains everything of interest about the object. Chapter 7 52

Method tostring, cont. example public String tostring(); { return ( Name: + getname()+ \n + Number: + getnumber()); } Whenever a new class is created, a suitable tostring method should be defined. Chapter 7 53

Method tostring, cont. Method tostring can be called my the conventional means, Object_Name.toString, or by using only Object_Name. example System.out.println(s.toString()); or System.out.println(s); Chapter 7 54

Abstract Classes An abstract class is not intended to be used to create objects. Chapter 7 70

Abstract Classes, cont. By declaring one or more methods to be abstract and by omitting the method body, only objects of derived classes which override the method(s) can be instantiated. example public abstract void drawhere(); A class that has at least one abstract method must be declared abstract. Chapter 7 71

Abstract Classes, cont. class Figure Chapter 7 73

Abstract Classes, cont. An abstract class serves as a placeholder. An abstract class makes it simpler to define derived classes. An abstract class assures that all its derived classes implement its abstract method(s), or they too will be abstract. Chapter 7 74

Interfaces An interface specifies the headings for methods that must be defined for any class that implements the interface. Chapter 7 75

Interfaces, cont. Interface Writeable Chapter 7 76

Interfaces, cont. To implement an interface, a class must include the phrase implements Interface_Name at the start of the class definition example implements MyInterface, YourInterface implement all the method headings listed in the definition of the interface. Chapter 7 77

Interfaces, cont. class WriteableUndergraduate Chapter 7 78

Interfaces, cont. An interface is a type. A method may have a parameter of an interface type. Any class that implements the interface can be substitute for the parameter. A class can implement any number of interfaces by providing implementations for all the methods of all the interfaces it claims to implement. Chapter 7 79

Dynamic Binding Different objects can invoke different method definitions using the same method name. For example, if b references a Box and t references a Triangle, b and t invoke different definitions of method drawat even if b and t are declared to be objects of type Figure. Chapter 7 81

Dynamic Binding, cont. Handling the invocation of a method that may be overridden later is called dynamic binding or late binding. The type of object being referenced at the time of the method call, not the type of reference that was declared, determines which method is invoked. Chapter 7 82

Dynamic Binding, cont. Consider Figure f; Box b = new Box(1, 4, 4); f = b; f.drawat(2); Triangle t = new Triangle(1,2); f = t; f.drawat(2); Chapter 7 83

Dynamic Binding, cont. Method drawat is inherited from class Figure and is not overridden. But, method drawhere is invoked within the definition of method drawat, and method drawhere is overridden. The type of object referred to by f determines which method drawhere is invoked. Chapter 7 84

Dynamic Binding with tostring Recall method tostring typically is used to prepare and return a string, describing an object, for output to the screen. The name of this method can be omitted, thanks to dynamic binding, because one definition of method println expects a single argument of type Object which it uses to invoke the method tostring associated with the object. Chapter 7 90

Polymorphism Polymorphism comes from Greek meaning many forms. In Java, polymorphism refers to the dynamic binding mechanism that determines which method definition will be used when a method name has been overridden. Thus, polymorphism refers to dynamic binding. Chapter 7 91

Subtle Difference Dynamic binding refers to the process carried out by the computer. Polymorphism can be thought of as something objects do. Polymorphism, encapsulation, and inheritance, and considered to be the main features of object-oriented programming. Chapter 7 92

The Class JApplet An applet is a derived class from the class JApplet public class LabelDemo extends JApplet Class JApplet has methods named init and paint. When you define methods init or paint in an applet, you are overriding inherited methods. Chapter 7 98

The Class JApplet, cont. Methods init and/or paint can be invoked without having to (re)define them. Thanks to polymorphism, methods init and/or paint defined in an applet will be invoked by library class methods and other methods when you run the applet with the applet (of type JApplet) as the parameter to the method. Chapter 7 99

The Class JApplet, cont. The class JApplet is used as a base class to derive applets to run from a webpage. Chapter 7 100

The Class JFrame A Graphical User Interface or GUI is simply a windowing interface for some kind of program. The class JFrame, rather than the class JApplet, is used to produce GUIs that can run as regular Java applications. Chapter 7 101

The Class Jframe, cont. class ButtonDemo Chapter 7 102

The Class Jframe, cont class ShowButtonDemo Chapter 7 103

The Class JFrame, cont. Chapter 7 104

The Class JFrame, cont. The class JFrame, and every class derived from it, has a method named setvisible. When its argument has the value true, the GUI is visible. Labels, buttons, JFrames, and other components inherit method setvisible from a common ancestor. Chapter 7 105

The Class JFrame, cont. A class derived from JFrame has no init method, but it does use a constructor. Code that would be in the init method of an applet is placed in the constructor of the class derived from JFrame. A GUI derived from a JFrame sets an initial size setsize(width, HEIGHT); Chapter 7 106

Window Events and Window Listeners With a JFrame, a GUI s close-window button needs to be programmed. A close-window button generates a window event which is handled by a window listener. Class WindowAdapter is a window listener, so every class derived from class WindowAdapter is a window listener. Chapter 7 107

Window Events and Window Listeners, cont. A window listener is registered with a JFrame GUI using method addwindowlistener. Chapter 7 108

Window Events and Window class WindowDestroyer Listeners, cont. Chapter 7 109

Window Events and Window Listeners, cont. A WindowDestroyer object is created and registered as a window listener for our JFrame GUI using WindowDestroyer listener = new WindowDestroyer(); addwindowlistener(listener); When the window-close button is clicked, the JFrame GUI ends. Chapter 7 110

The ActionListener Interface The ActionListener interface has only one method heading that must be implemented. public void actionperformed (ActionEvent e) A listener that responds to button clicks in an applet or in a JFrame must implement the ActionListener interface. Chapter 7 111

Programming Example: Smiley Face as a JFrame Class JFrame and every class derived from class JFrame has a paint method. The paint method can be redefined to draw a figure. Chapter 7 112

Programming Example: Smiley Face as a Jframe, class HappyFace cont. Chapter 7 113

Programming Example: Smiley Face as a Jframe, class ShowHappyFace cont. Chapter 7 114

Programming Example: Smiley Face as a Jframe, cont. Chapter 7 115