Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism in Methods
Chapter 13 - Inheritance.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Inheritance in Java.
ATS Application Programming: Java Programming
Java Programming Language
Polymorphism and access control
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
More About Inheritance & Interfaces
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

Polymorphism and access control

RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability to make identical method calls invoke different behaviors This is one of the fundamental concepts in Object-Oriented programming! Enables code reuse, encapsulation, etc.

RHS – SOC 3 What is polymorphism? We have already seen it – more or less Can be achieved either through interfaces or inheritance (no fundamental difference) The principle is the same – a method is defined in a fundamental class, and several other classes can contain different implementations of that method

RHS – SOC 4 What is polymorphism? Animal + makeSound() Duck + makeSound() Mouse + makeSound()

RHS – SOC 5 What is polymorphism? public class Duck extends Animal { public void makeSound() { print(”Quack”);} } public class Mouse extends Animal { public void makeSound() { print(”Squeak”);} }

RHS – SOC 6 What is polymorphism? public void makeAnimals() { ArrayList zoo; zoo = new ArrayList (); zoo.add(new Duck()); zoo.add(new Mouse()); zoo.add(new Duck()); zoo.add(new Mouse()); makeNoise(zoo); }

RHS – SOC 7 What is polymorphism? public void makeNoise(ArrayList zoo) { for (Animal a : zoo) a.makeSound(); } Output: ”Quack” ”Squeak” ”Quack” ”Squeak”

RHS – SOC 8 What is polymorphism? A bit strange, when you think of it… The method makeNoise knows nothing at all about the subclasses… …but still, the subclass-specific methods are invoked! How is that possible?

RHS – SOC 9 What is polymorphism? ”In Java, method calls are always determined by the type of the actual object, not the type of the object reference” This is polymorphism

RHS – SOC 10 What is polymorphism? The method makeNoise will still work, even if we add new subclasses! (”closed for modification, open for extension”) It is the Java Virtual Machine (JVM), which picks the correct method to call The JVM knows the true type of any object This principle is know as late binding

RHS – SOC 11 Choosing methods Multiple versions of the same method can also be create through overloading println(String output) println(int output) However, method ”signatures” are different Compiler can pick correct method at compile-time, this is early binding

RHS – SOC 12 Abstract classes An interface class only provides method definitions, no implementation at all A super-class provides an implementation for all its methods, which can be over- written or used as-is Not the entire truth… We can choose only to provide an imple- mentation for some of the methods

RHS – SOC 13 Abstract classes public class DefaultShape { public void draw() { // do nothing } public double getArea() { return 0.0;} }

RHS – SOC 14 Abstract classes public abstract class DefaultShape { public void draw() { // do nothing } public abstract double getArea(); } This is known as an abstract class getArea is an abstract method

RHS – SOC 15 Abstract classes You can never create an instance of an abstract class (just like interface) You can have a variable of this type You force the user to provide an implementation of the abstract methods

RHS – SOC 16 Abstract classes Is an abstract class just an interface? No, because they can have –Instance fields –Methods with implementation –Constructors Why constructors…? –Cannot be called by a user, but… –…can be called from a sub-class

RHS – SOC 17 Preventing subclassing Maybe we do not want a user to be able to create a sub- class, or override a method Could be for security reasons, etc. We can prevent this by declaring a class or method as being final

RHS – SOC 18 Preventing subclassing public class SecureAccount extends BankAccount {... public final boolean checkPassword(String pwd) {... } } public final class String {...}

RHS – SOC 19 Access control Why could we not do this…? // Overridden method in CheckingAccount public void deposit(double amount) { balance = balance + amount; transactionCount++; } private!

RHS – SOC 20 Access control Even a sub-class does not gain access to private methods or instance fields Private really means private! Sub-classes must use get/set-methods, just like any other user Not specifying public or private will provide access at package level

RHS – SOC 21 Access control public class Person {// Access private String name; // class only public int height; // everybody int weight; // all in package } Unless a very good reason exists, stick to public and private…

RHS – SOC 22 The Object class The system class Object is a superclass of all classes in Java! Polymorphism to the extreme!? Which methods could be meaningful to all Java classes?

RHS – SOC 23 The Object class Not many… String toString() – returns a string representation of the object boolean equals(Object other) – tests if the object equals another object Object clone() – makes a full copy of the object

RHS – SOC 24 The Object class String toString() –Mostly used for debugging –If not overwritten, outputs the class name plus an object identifier –Usually overwritten to print out the values of the instance fields, i.e. the state of the object –Subclasses can print their own part

RHS – SOC 25 The Object class boolean equals(Object other) –Often useful to know if two objects are ”equal” –Equal by content, not by reference! –We must first cast argument to correct class, then check if instance fields are equal –NOTE: For instance fields of non-primitive types, we need to call their equals(...) methods…

RHS – SOC 26 The Object class Object clone() –Useful, if we want a real copy of an object, not just a copy of the reference to the object –Clone() method should return a new object, with the same state as the given object –Not trivial to clone an object, if the object contains references to other objects –Can make a shallow copy or deep copy

RHS – SOC 27 The Object class Original Org. Copy Original Org. Copy Deep copy Shallow copy

RHS – SOC 28 Exercises Self-check: 6, 15 Review: R10.5 Programming: P10.5, P10.6