Intro to OOP with Java, C. Thomas Wu

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to OOP with Java 4th Ed, C. Thomas Wu
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
OOP: Inheritance By: Lamiaa Said.
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 In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved 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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism Animated Version.
©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 and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Chapter 10: Inheritance and Polymorphism
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance using Java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Inheritance and Polymorphism.
Inheritance.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Intro to OOP with Java, C. Thomas Wu INHERITANCE ©The McGraw-Hill Companies, Inc.

Defining Classes with Inheritance Intro to OOP with Java, C. Thomas Wu Defining Classes with Inheritance Case Study: Suppose we want implement a class roster that contains both undergraduate and graduate students. Each student’s record will contain his or her name, three test scores, and the final course grade. The formula for determining the course grade is different for graduate students than for undergraduate students. Undergrads: pass if avg test score >= 70 Grads: pass if avg test score >= 80 ©The McGraw-Hill Companies, Inc.

Modeling Two Types of Students Intro to OOP with Java, C. Thomas Wu Modeling Two Types of Students There are two ways to design the classes to model undergraduate and graduate students. We can define two unrelated classes, one for undergraduates and one for graduates. We can model the two kinds of students by using classes that are related in an inheritance hierarchy. Two classes are unrelated if they are not connected in an inheritance relationship. ©The McGraw-Hill Companies, Inc.

Classes for the Class Roster Intro to OOP with Java, C. Thomas Wu Classes for the Class Roster For the Class Roster sample, we design three classes: Student UndergraduateStudent GraduateStudent The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects. The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects. ©The McGraw-Hill Companies, Inc.

Inheritance Hierarchy Intro to OOP with Java, C. Thomas Wu Inheritance Hierarchy + computeCourseGrade() : int + UndergraduateStudent() : void + GraduateStudent() : void + computeCourseGrade() : int + computeCourseGrade() : int ©The McGraw-Hill Companies, Inc.

Definition of GraduateStudent & UndergraduateStudent classes Intro to OOP with Java, C. Thomas Wu Definition of GraduateStudent & UndergraduateStudent classes class GraduateStudent extends Student { //constructor not shown public void computeCourseGrade() { int total = 0; total = test1 + test2 + test3; if (total / 3 >= 80) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } } } class UndergraduateStudent extends Student { //Constructor not shown public void computeCourseGrade() { int total = 0; total = test1 + test2 + test3; if (total / 3 >= 70) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } } } ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu Declaring a Subclass A subclass inherits data and methods from the superclass. In the subclass, you can also: Add new data Add new methods Override the methods of the superclass Modify existing behaviour of parent ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu Inheritance Rules The private members of the superclass are private to the superclass The subclass can access the members of the superclass according to the accessibility rules The subclass can include additional data and/or method members ©The McGraw-Hill Companies, Inc.

Inheritance Rules (continued) Intro to OOP with Java, C. Thomas Wu Inheritance Rules (continued) 4. The subclass can override, that is, redefine the methods of the superclass The overriding method in subclass must have similar Name Parameter list Return type 5. All members of the superclass are also members of the subclass Similarly, the methods of the superclass (unless overridden) are also the methods of the subclass Remember Rule 1 & 2 when accessing a member of the superclass in the subclass ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu Inheritance Rules (continued) 6. (Using the Keyword super) The keyword super refers to the direct superclass of a subclass . This keyword can be used in two ways: To call a superclass constructor super(); //must be the first statement in subclass’s constructor To call a superclass method super.methodname(); this is only used if the subclass overrides the superclass method ©The McGraw-Hill Companies, Inc.

(Accessibility Modifier) Intro to OOP with Java, C. Thomas Wu INHERITANCE (Accessibility Modifier) Sometimes , it is called visibility modifier Not all properties can be accessed by sub class. Super class can control a data accessing from subclass by giving the type of accessing to the members and methods. A class can declare the data members or method as a public, private or protected. If it is not declared, the data or method will be set to default type. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu INHERITANCE Data Accessibility Super class int a public int b protected int c private int d Package A Package B Sub class B public int b protected int c Sub class A int a public int b protected int c ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu Refer to the previous slide Super class has 2 subclasses : Subclass A and Subclass B. Subclass A is defined in same package with superclass, subclass B is defined outside the package. There are 4 accessibility data types: public, protected, private and default. Subclass A can access all properties of superclass except private. But, subclass B can only access the properties outside the package which are public and protected. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu class ClassX { private int m; public String toString() return new String("(" + m + ")"); } public class ClassY extends ClassX private int n; return new String("(" + m + " , " + n + ")"); class TestAccesibility public static void main(String [] args) ClassX x = new ClassX; ClassY y = new ClassY; System.out.println("x = " + x); System.out.println("y = " + y); What’s wrong with the code? How to fix it? ©The McGraw-Hill Companies, Inc.

Inheritance and Constructors Intro to OOP with Java, C. Thomas Wu Inheritance and Constructors Unlike members of a superclass, constructors of a superclass are not inherited by its subclasses. You must define a constructor for a class or use the default constructor added by the compiler. The statement super(); calls the superclass’s constructor. super(); must be the first statement in the subclass contructor. ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu A call to the constructor of the superclass must be in the first statement in the child constructor. public Box(double l, double w, double h) { super(l,w); height = h; } ©The McGraw-Hill Companies, Inc.

Intro to OOP with Java, C. Thomas Wu Rectangle myRectangle = new Rectangle(5, 3); Box myBox = new Box(6, 5, 4); ©The McGraw-Hill Companies, Inc.

Superclass’s Constructor Is Always Invoked Intro to OOP with Java, C. Thomas Wu Superclass’s Constructor Is Always Invoked A subclass constructor may invoke its superclass’s constructor. If none is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, the constructor of class A: ©The McGraw-Hill Companies, Inc.

Example on the Impact of a Superclass without no-arg Constructor Intro to OOP with Java, C. Thomas Wu Example on the Impact of a Superclass without no-arg Constructor Find out the error in the program: class Fruit { public Fruit(String name) { System.out.println("Fruit constructor is invoked"); } public class Apple extends Fruit { public Apple(String name) { System.out.println(“Apple constructor is invoked");   ©The McGraw-Hill Companies, Inc.