Chapter 11 Inheritance and Encapsulation and Polymorphism

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
What is inheritance? It is the ability to create a new class from an existing class.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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,
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 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
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.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Inheritance.
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Object Oriented Programming
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Lecture 11: More on Classes
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Classes and Objects, State and Behavior
Lecture 8-3: Encapsulation, this
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
Chapter 9 Inheritance and Polymorphism
Topic 29 classes and objects, part 3
Building Java Programs
Java Inheritance.
Building Java Programs
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Chapter 14 Abstract Classes and Interfaces
Building Java Programs
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
Building Java Programs
Final and Abstract Classes
Building Java Programs
Building Java Programs
Building Java Programs
Topic 29 classes and objects, part 3
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

Chapter 11 Inheritance and Encapsulation and Polymorphism

Declaring a Subclass A subclass extends properties and methods from the superclass. You can also: Add new properties Add new methods Override the methods of the superclass

Superclasses and Subclasses GeometricObject1 Circle4 Rectangle1 TestCircleRectangle Run

Are superclass’s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.

Using the Keyword super The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor To call a superclass method

CAUTION You must use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor.

Multiple Inheritance A very important fact to remember is that Java only supports only single inheritance. This means that a class cannot extend more than one class. Therefore following is illegal:

Multiple Inheritance However, C++ provides the ability to do multiple inheritance. Multiple inheritance enables a derived class to inherit members from more than one parent. To provide Multiple Inheritance functionality Java uses Interfaces. A class can implement one or more interfaces.

Overriding vs. Overloading

The toString() method in Object The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. Loan loan = new Loan(); System.out.println(loan.toString()); The code displays something like Loan@15037e5 . This message is not very helpful or informative. Usually you should override the toString method so that it returns a digestible string representation of the object.

The instanceof Operator Use the instanceof operator to test whether an object is an instance of a class: Object myObject = new Circle(); // Some lines of code /** Perform casting if myObject is an instance of Circle */ if (myObject instanceof Circle) { System.out.println("myObject is instance of Circle"); ... }

Encapsulation encapsulation: Hiding implementation details from clients. Encapsulation forces abstraction. separates external view (behavior) from internal view (state) protects the integrity of an object's data

A field that cannot be accessed from outside the class Private fields A field that cannot be accessed from outside the class private type name; Examples: private int id; private String name; Client code won't compile if it accesses private fields:

Accessing private state

Accessing private state // A "read-only" access to the x field public int getX() { return x; } // Allows clients to change the x field public void setX(int newX) { x = newX; Client code will look more like this: System.out.println(p1.getX()); p1.setX(14);

Example: Point class // A Point object represents an (x, y) location. public class Point { private int x; private int y; public Point(int initialX, int initialY) { x = initialX; y = initialY; } public int getX() { return x; public int getY() { return y; public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); public void setLocation(int newX, int newY) { x = newX; y = newY; public void translate(int dx, int dy) { setLocation(x + dx, y + dy);

Benefits of encapsulation Abstraction between object and clients Protects object from unwanted access Example: Can't fraudulently increase an Account's balance. Can constrain objects' state (invariants) Example: Only allow Accounts with non-negative balance. Example: Only allow Dates with a month from 1-12.

The ArrayList Class You can create an array to store objects. But the array’s size is fixed once the array is created. Java provides the ArrayList class that can be used to store an unlimited number of objects.

ArrayList Example TestArrayList Run

The protected Modifier The protected modifier can be applied on data and methods in a class. A protected data or a protected method in a public class can be accessed by any class in the same package or its subclasses. Cannot be accessed if the subclasses are in a different package. private, default, protected, public

Accessibility Summary

Visibility Modifiers

NOTE the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method.

The final Modifier The final class cannot be extended: final class Math { ... } The final variable is a constant: final static double PI = 3.14159; The final method cannot be overridden by its subclasses.

Polymorphism Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

Polymorphism Pig from Animal object Dog from Animal object class Animal {   public void animalSound() {     System.out.println("The animal makes a sound");   } } class Pig extends Animal {   public void animalSound() {     System.out.println("The pig says: wee wee");   } } class Dog extends Animal {   public void animalSound() {     System.out.println("The dog says: bow wow");   } } Pig from Animal object class MyMainClass {   public static void main(String[] args) {     Animal myAnimal = new Animal();  // Create a Animal object     Animal myPig = new Pig();  // Create a Pig object     Animal myDog = new Dog();  // Create a Dog object     myAnimal.animalSound();     myPig.animalSound();     myDog.animalSound();   } } Dog from Animal object