1 More About Derived Classes and Inheritance Chapter 9.

Slides:



Advertisements
Similar presentations
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,
Advertisements

Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 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.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
Topic 4 Inheritance.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
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.
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Object-Oriented Programming: Polymorphism Chapter 10.
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.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
One class is an extension of another.
03/10/14 Inheritance-2.
Chapter 11: Inheritance and Polymorphism
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Can perform actions and provide communication
One class is an extension of another.
Inheritance Chapter 5.
Can perform actions and provide communication
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Can perform actions and provide communication
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism
Chapter 8 Inheritance Part 2.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

1 More About Derived Classes and Inheritance Chapter 9

The Principle of Encapsulation Normally we don't want instance variables of a class to be accessible to methods in other classes. The access modifier private makes an instance variable or method inaccessible to methods in other classes. If we want other classes to be able to see the value of an instance variable we can provide a public accessor method. What about derived classes? A private member is inaccessible even to methods in derived classes. 2

Access Modifiers for Derived Classes Derived classes have a special relationship to their base classes. The base class is effectively a part of the derived class. We typically want a derived class to have access to members of the base class that are not accessible to the general public. We can do this with the protected access modifier. 3

Recall our hierarchy of shapes from last class. 4 Shape Triangle Rectangle Circle "is a"

Another Shape Suppose now that we need a class Square. Clearly a square "is a" rectangle. A square is a special case of a rectangle. Length = Width. We can use a square wherever we need a rectangle. Class Square should be a subclass of class Rectangle. 5

Class Square Class Square should be a subclass of class Rectangle. But it doesn't have any new members. It has a restriction. Length must equal width. 6

Download Shape files from the class web site Downloads/2016_04_25_Shapes/ We will add Square.java 7

Implementing Class Square Look at Rectangle.java // // Constructor - Initializes instance variables // public Rectangle(double length, double width) { super("Rectangle", 4, length*width); this.name = "Rectangle"; this.length = length; this.width = width; } 8 If derived class Square invokes this constructor, the name will be set to Rectangle, not Square.

New Rectangle Constructor In order to support the derived class Square, we need a new constructor in class Rectangle. Overload the constructor. Let the caller specify the name. 9

New Rectangle Constructor // // Constructor for derived classes // protected Rectangle(String name, double length, double width) { super(name, 4, length*width); this.length = length; this.width = width; } protected access modifier means that this constructor can only be invoked from a derived class. 10

Class Square Create a new file for class Square. Square.java Content on next slide. 11

//************************************************ // Square.java // // Represents a geometrical square // //************************************************ public class Square extends Rectangle { // // Constructor // public Square(double side) { super("Square", side, side); } 12 Uses the new overloaded constructor Inherits toString method from class Rectangle

Compile 13

Add Test Code for Square if (shapeName.equals("Rectangle")) { double length, width; System.out.print ("Length: "); length = keyboardScanner.nextDouble(); System.out.print ("Width: "); width = keyboardScanner.nextDouble(); System.out.println(); // Create a Rectangle object new_shape = new Rectangle(length, width); } else if (shapeName.equals("Square")) { double side; System.out.print("Side: "); side = keyboardScanner.nextDouble(); new_shape = new Square(side); System.out.println(); } 14

Comment out test code for Triangle else if (shapeName.equals("Triangle")) { // double base, height; // System.out.print("Base: "); // base = keyboardScanner.nextDouble(); // System.out.print ("Height: "); // height = keyboardScanner.nextDouble(); // new_shape = new Triangle(base, height); // System.out.println(); new_shape = null; } 15

Test Class Square 16 End of Section

Shape Objects It really doesn't make sense to instantiate a generic Shape object. Only the derived classes represent real shapes. The only purpose of class Shape is to serve as a base class for the derived classes. A single home for everthing that is common to all classes. Java has provision for classes like this: Abstract class 17

Abstract Class Textbook, page 461 The modifier abstract in the class header tells the compiler that this is to be an abstract class. Cannot be instantiated. Exists only to be a base class for derived classes. Let's make the Shape class abstract. 18

//************************************************ // Shape.java // // Represents a geometrical shape // //************************************************ public abstract class Shape { private static int nr_shape_objects = 0; // Instance variables private String name; private int id; private int nr_sides; private double area;... 19

Recompile and Test 20 We have to revise Shape_Tester.java now. It can no longer instantiate generic Shape objects.

Update Shape_Test.java 21

Revised Shape_Tester 22

Abstract Methods An abstract class can include abstract methods. Methods defined only to be overridden in derived classes. Why do this? It guarantees that any object of a derived class will have an implementation of that method even though the base class cannot provide a meaningful definition. A method in another class with a reference to an object of the base class can invoke the method without concern for which derived class it is. Polymorphism! 23

Abstract Methods How do we make a method abstract? Let the class definition have a declaration for the method, but no definition. Include the keyword abstract in the method declaration. Example: abstract double perimeter(); 24

Abstract Methods If a an abstract base class includes an abstract method, every derived class must provide an implementation of the method. Or else be an abstract class itself. Every object of a derived class will have the method. 25

Abstract Methods Let's add an abstract Perimeter method to class Shape. //************************************************ // Shape.java // // Represents a geometrical shape // //************************************************ public abstract class Shape {... // // Returns a the perimeter of the Shape // abstract double Perimeter();... 26

Compiling 27 We must implement the Perimeter method in class Rectangle.

Class Rectangle Add to class Rectangle: public double Perimeter() { return 2.0*length + 2.0*width; } 28 Now it compiles again.

Add test code 29 Add to Shape_Tester.java:

Testing 30 What about Square?

Testing Class Square 31 Class Square inherits the Perimeter method from its base class, Rectangle.

Testing Class Circle 32 Class Circle doesn't inherit the Perimeter method, and does not implement it.

Add to Class Circle public double Perimeter() { return 2.0*Math.PI*radius; } 33

Perimeter of a Circle 34

Summary An abstract class exists only to be the base class for derived classes. Cannot be instantiated. An abstract class can define abstract methods. No definition, just a declaration. Each derived class must provide a definition unless it is also abstract. Every object of a class derived from the abstract class will have the abstract methods. 35