Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.

Slides:



Advertisements
Similar presentations
ICS 201 Inheritance Introduction to Computer Science
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
What is inheritance? It is the ability to create a new class from an existing class.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Programming With Java ICS Chapter 8 Polymorphism.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
成都信息工程学院 计算机系 1 Java 程序设计 Java Programming Fall, 2010.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Advanced Java Topics Chapter 9
Lecture 12 Inheritance.
Objects as a programming concept
Inheritance and Polymorphism
Polymorphism 2nd Lecture
UML Class Diagram: class Rectangle
Polymorphism 2nd Lecture
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance

Programming With Java ICS201 Inheritance Hierarchy

Programming With Java ICS201 Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class  An instance variable that is private in a base class is not accessible by name in the definition of a method in any other class, not even in a method definition of a derived class

Programming With Java ICS201 Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class  Instead, a private instance variable of the base class can only be accessed by the public accessor and mutator methods defined in that class

Programming With Java ICS201 Access Modifiers  public Can be used in any Java program without restriction  private may only be used by the instance of the class that declares the variable or method  Protected available to all classes in the same package available to all subclasses( even those in different packages )

Programming With Java ICS201 What is Package ?  Way to group a related class into one unit  To resolve the name conflicts between class names  Include all the classes in package import packageName.*; import packageName.className ;

Programming With Java ICS201 Some Predefined Java Packages Package NameContents java.appletClasses for implementing applets java.awtClasses for graphics, windows, and GUI’s java.awt.eventClasses supporting AWT event handling java.awt.imageClasses for image handling java.ioClasses for input and output java.langBasic language classes like Math (always available in any Java program) java.netClasses for networking java.utilUseful auxiliary classes like Date

Programming With Java ICS201 Visibility and Inheritance VisibilityPublicdefaultprotectedprivate Clients in same package CCCNone Clients in different packages CNone Subclass in same package CCCNone Subclass in different package CNoneC Note: C; Can access default: If the access modifier is omitted

Programming With Java ICS201 © 2006 Pearson Addison-Wesley. All rights reserved7-9 Access Modifiers

Programming With Java ICS201 The class Object  All classes defined without an explicit extends clause automatically extend Object The Object Class is the Superclass of Every Java Class

Programming With Java ICS201 The Class Object  The class Object is in the package java.lang which is always imported automatically public class Circle Equivalent public class Circle extends Object  Most useful methods: String toString() boolean equals(Object otherObject) Object clone()  Good idea to override these methods in your own classes

Programming With Java ICS201 Define equals See Object2.java 7-12

Programming With Java ICS201 Object-Oriented Concept  Object & Class  Encapsulation  Inheritance  Polymorphism 13

Programming With Java ICS Polymorphism  polymorphism: many forms

Programming With Java ICS201 University Of Ha’il15 Polymorphism  Polymorphism (from the Greek, meaning "many forms", or something similar)  The mechanism by which several methods can have the same name and implement the same abstract operation. Requires that there be multiple methods of the same name The choice of which one to execute depends on the object that is in a variable Reduces the need for programmers to code many if- else or switch statements

Programming With Java ICS201 University Of Ha’il16 Polymorphism  Methods can be overloaded. A method has the same name as another member methods, but the type and/or the count of parameters differs. class Integer { float val; void add( int amount ) { val = val + amount; } void add( int num, int den) { val = float(num) / den; }

Programming With Java ICS201 Polymorphism  Add(integer, integer)  Add(string, string)  Add(string, integer) Add(1,1)  2 Add(“Hello”, “World”)  “HelloWorld” Add(“Hello”, 2)  “Hello2” 17

Programming With Java ICS201 Polymorphism :PaySlip :HourlyPaidEmployee :WeeklyPaidEmployee :MonthlyPaidEmployee  getTotalPay() calculatePay() 18