(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance Chapter 9.
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 8 More Object Concepts
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
© 2013 Ken Howard, Southern Methodist University Object-Oriented Programming: Inheritance CSE 1341.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
(C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Topics Inheritance introduction
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Advanced Java Topics Chapter 9
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Chapter 9 Object-Oriented Programming: Inheritance
Presentation transcript:

(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e

(C) 2010 Pearson Education, Inc. All rights reserved. Overview  Inheritance  Superclasses and Subclasses  Keyword extend  The protected Access Modifier  Access superclass members with super  Constructors in inheritance hierarchies

(C) 2010 Pearson Education, Inc. All rights reserved.  Inheritance  A form of software reuse in which a new class is created by absorbing an existing class’s members and embellishing them with new or modified capabilities. Introduction

(C) 2010 Pearson Education, Inc. All rights reserved.  When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class.  Existing class is the superclass  New class is the subclass  Each subclass can be a superclass of future subclasses.  A subclass can add its own fields and methods.  A subclass is more specific than its superclass and represents a more specialized group of objects.  The subclass exhibits the behaviors of its superclass and can add behaviors that are specific to the subclass.  This is why inheritance is sometimes referred to as specialization. Introduction

(C) 2010 Pearson Education, Inc. All rights reserved.  The direct superclass is the superclass from which the subclass explicitly inherits.  An indirect superclass is any class above the direct superclass in the class hierarchy.  The Java class hierarchy begins with class Object  Java supports only single inheritance, in which each class is derived from exactly one direct superclass. Introduction (Cont'd)

(C) 2010 Pearson Education, Inc. All rights reserved.  We distinguish between the is-a relationship and the has-a relationship  Is-a represents inheritance  In an is-a relationship, an object of a subclass can also be treated as an object of its superclass  Has-a represents composition  In a has-a relationship, an object contains as members references to other objects Introduction (Cont'd)

(C) 2010 Pearson Education, Inc. All rights reserved.  Superclasses tend to be “more general” and subclasses “more specific.” Superclass and Subclass

(C) 2010 Pearson Education, Inc. All rights reserved.

 Given the classes Employee, BirthDate and TelephoneNumber.  Inheritance or Composition? Inheritance or Composition?

(C) 2010 Pearson Education, Inc. All rights reserved. A subclass can inherit methods that it does not need or should not have.  Even when a superclass method is appropriate for a subclass, that subclass often needs a customized version of the method.  The subclass can override (redefine) the superclass method with an appropriate implementation. Inheritance Issue

(C) 2010 Pearson Education, Inc. All rights reserved.  protected access is an intermediate level of access between public and private.  A superclass’s protected members can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same package Protected Members

(C) 2010 Pearson Education, Inc. All rights reserved.  When a subclass method overrides an inherited superclass method, the superclass method can be accessed from the subclass by preceding the superclass method name with keyword super and a dot (. ) separator. Protected Members (cont'd)

(C) 2010 Pearson Education, Inc. All rights reserved.  To override a superclass method, a subclass must declare a method with the same signature as the superclass method annotation  Indicates that a method should override a superclass method with the same signature.  If it does not, a compilation error occurs. Overriding Superclass Methods

(C) 2010 Pearson Education, Inc. All rights reserved.  Instantiating a subclass object begins a chain of constructor calls  The subclass constructor, before performing its own tasks, invokes its direct superclass’s constructor  If the superclass is derived from another class, the superclass constructor invokes the constructor of the next class up the hierarchy, and so on.  The last constructor called in the chain is always class Object ’s constructor.  Original subclass constructor’s body finishes executing last.  Each superclass’s constructor manipulates the superclass instance variables that the subclass object inherits. Constructor in Subclasses

Access Superclass methods  To access a superclass method: ◦ Use the keyword super() plus a dot and then the name of the method. For example: ◦ super().someSuperclassMethod()

(C) 2010 Pearson Education, Inc. All rights reserved.  All classes in Java inherit directly or indirectly from Object, so its 11 methods are inherited by all other classes.  Every array has an overridden clone method that copies the array.  If the array stores references to objects, the objects are not copied—a shallow copy is performed. Object class

Questions?

(C) 2010 Pearson Education, Inc. All rights reserved.  Labels are a convenient way of identifying GUI components on the screen and keeping the user informed about the current state of the program.  A JLabel (from package javax.swing ) can display text, an image or both.  The example in Fig demonstrates several JLabel features, including a plain text label, an image label and a label with both text and an image.

(C) 2010 Pearson Education, Inc. All rights reserved.

 An ImageIcon represents an image that can be displayed on a JLabel.  The constructor for ImageIcon receives a String that specifies the path to the image.  ImageIcon can load images in GIF, JPEG and PNG image formats.  JLabel method setText changes the text the label displays.

(C) 2010 Pearson Education, Inc. All rights reserved.  An overloaded version of method add that takes two parameters allows you to specify the GUI component to add to a JFrame and the location in which to add it.  The first parameter is the component to attach.  The second is the region in which it should be placed.  Each JFrame has a layout to position GUI components.  Default layout for a JFrame is BorderLayout.  Five regions— NORTH (top), SOUTH (bottom), EAST (right side), WEST (left side) and CENTER (constants in class BorderLayout )  Each region is declared as a constant in class BorderLayout.  When calling method add with one argument, the JFrame places the component in the BorderLayout ’s CENTER automatically.