L EC. 07: I NHERITANCE 0. 2015 S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
L EC. 06: C LASS D ETAILS S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
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.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Inheritance class TwoDShape { private double width;
Inheritance and Polymorphism
C# - Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Modern Programming Tools And Techniques-I Inheritance
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Polymorphism
CS 200 More Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism Part 1
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

L EC. 07: I NHERITANCE 0

2015 S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references and subclass objects  Hiding and overriding inherited members  Method overriding  Polymorphism [ 程設 4]  Abstract classes and methods [ 程設 4]  Final [ 程設 4]  Blueprint of java.lang.Object class [ 程設 4] 1

I NHERITANCE B ASICS  Inheritance is one of 4 major OOP features.  Inheritance allows a class to use the codes, including fields and methods, defined in a pre-exist class without recoding while adding new features.  With inheritance a class can be derived from other pre-exist classes, thereby inherit fields and methods from those classes.  A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).  The class from which a subclass is derived is called a superclass (also a base class or parent class).  A superclass is more abstract or generic than its subclasses and a subclass is more specific than its superclass. 2

I NHERITANCE  Inheritance creates the “is-a” relationship between two classes: subclass is-a superclass, which means a subclass is a special case of its superclass.  Each subclass object is an object of the superclass,,e.g. a manager is a fulltime employee and a fulltime employee is an employee.  A superclass object may not be an object of its subbclass, e.g. an employee may not be a fulltime employee and a fulltime employee may not be a manager. 3

U SING KEYWORD extends  In Java, the keyword extends is used to specify the relationship between a subclass and its superclass.  Syntax form class extends { // class body } 4

E XAMPLE 5 class Vehicle { public void start() { System.out.println("Starting..."); } class Car extends Vehicle { public void drive() { System.out.println("Driving..."); } class App217 { public static void main(String[] args) { Car c = new Car(); c.start(); c.drive(); } + start() is-a vehicle + drive() car Starting... Driving... For efficient reuse UML 父 子 ♥

E XAMPLE class TwoDShape { double width; double height; void showDim() { System.out.println("Width and height are " + width + " and " + height); } class Rectangle extends TwoDShape { boolean isSquare() { if(width == height) return true; return false; } double area() { return width * height; } 6 double width double height void showDim() is-a TwoDShape boolean isSquare() double area() Rectangle Architecture design

class Triangle extends TwoDShape { String style; double area() { return width * height / 2; } void showStyle() { System.out.println("Triangle is " + style); } 7 double width double height void showDim() is-a TwoDShape String style double area() void showStyle() Triangle is-a boolean isSquare() double area() Rectangle common 單一繼承

class Shapes { public static void main(String args[]) { Rectangle r1 = new Rectangle(); Triangle t1 = new Triangle(); r1.width = 2.0; r1.height = 4.0; r1.showDim(); System.out.println("is a Square? " + r1.isSquare()); System.out.println("Area is " + r1.area()); t1.width = 4.0; t1.height = 4.0; t1.style = "filled"; t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); } 8 Width and height are 2.0 and 4.0 is a Square? false Area is 8.0 Triangle is filled Width and height are 4.0 and 4.0 Area is 8.0

E XERCISE 1A  Based on class TwoDShape, create a subclass called Volume. 利用 class Volume 來計算一個長方體的體積. 9 double width double height void showDim() TwoDShape double z; double vol() Volume

E XERCISE 1B class Ex2b { public static void main(String[] args) { Bird bird = new Bird(); Dog dog = new Dog(); Fish fish = new Fish(); bird.sleep(); bird.eat(); dog.sleep(); dog.eat(); fish.sleep(); fish.eat(); } 10 sleep() Animal eat() Bird eat() Dog eat() Fish An animal sleeps... A bird eats... An animal sleeps... A dog eats... An animal sleeps... A fish eats... is-a

M EMBER A CCESS AND INHERITANCE  All the public-mode fields and methods of a superclass can be inherited and directly referenced by any subclass of the superclass.  All the protected-mode fields and methods of a superclass can be inherited and directly referenced by any subclass of the superclass.  All the default-mode fields and methods of a superclass can be inherited and directly referenced by a subclass of the superclass if they are in the same package. 11

W HAT CAN BE INHERITED …  A subclass does not inherit the private members of its parent class.  If the superclass has public or protected methods for accessing its private fields, a subclass can access these members via these methods.  Since a nested class has access to all the private members of its enclosing class—both fields and methods, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. 12

E XAMPLE class TwoDShape { private double width; private double height; void showDim() { System.out.println("Width and height are " + width + " and " + height); } 13 ModifierSame Class Same Package SubclassUniverse privateYes defaultYes protectedYes publicYes protected double width; protected double height; double width; double height;

class Triangle extends TwoDShape { String style; double area() { return width * height / 2; } void showStyle() { System.out.println("Triangle is " + style); } class Shapes2 { public static void main(String args[]) { Triangle t1 = new Triangle(); t1.width = 4.0; t1.height = 4.0; t1.style = "filled"; t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); } 14 error - double width - double height void showDim() TwoDShape String style double area() void showStyle() Triangle

E XAMPLE class TwoDShape { private double width; private double height; double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } void showDim() { System.out.println("Width and height are " + width + " and " + height); } 15 getter setter

class Triangle extends TwoDShape { String style; double area() { return getWidth() * getHeight() / 2; } void showStyle() { System.out.println("Triangle is " + style); } class Shapes3 { public static void main(String args[]) { Triangle t1 = new Triangle(); t1.setWidth(4.0); t1.setHeight(4.0); t1.style = "filled"; t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); } 16

E XERCISE 2  把 Ex1A 的 instance width and height 設定為 private. 利 用 class Volume 來計算一個長方體的體積. 17

C ONSTRUCTORS AND INHERITANCE  When an object of a class is instantiated, the constructor of the class constructs its part and the constructor of the class’ superclass constructs the superclass portion of the object.  Remember the superclass has no knowledge of or access to any element in its subclass and the subclass has no responsibility to set inherited fields.  Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass  Invoking a superclass constructor can be performed only in constructors of associated subclasses. 18

E XAMPLE 1 class A { A() { System.out.println("In a's constructer..."); } class B extends A { B() { // super(); System.out.println("In b's constructer..."); } class App221 { public static void main(String[] args) { A objA = new A(); System.out.println("..."); B objB = new B(); } 19 In a's constructer In a's constructer... In b's constructer... A() is-a A B() B 父 子

E XAMPLE 2 class A { A() { System.out.println("In a's constructer..."); } class B extends A { B(String s) { // super(); System.out.println("In b's constructer..."); System.out.println(s); } class App222 { public static void main(String[] args) { B obj = new B("Hello from Java!"); // B obj = new B(); Error } 20 In a's constructer... In b's constructer... Hello from Java! + A() is-a A + B(String) B

class A { A() { System.out.println("In a's 1 st constructer..."); } A(String s) { System.out.println("In a's 2 nd constructer..."); System.out.println(s); } class B extends A { B() { // super(); System.out.println("In b's constructer..."); } class App223 { public static void main(String[] args) { B obj = new B(); } 21 In a's 1st constructer... In b's constructer... + A() + A(String) is-a A + B() B overload E XAMPLE 3 2 hr