Polymorphism and access control

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.
Inheritance Inheritance Reserved word protected Reserved word super
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
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]
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
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
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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 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.
Polymorphism in Methods
Chapter 13 - Inheritance.
Modern Programming Tools And Techniques-I
Data Structures and Algorithms revision
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Inheritance in Java.
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
More About Inheritance & Interfaces
Java Inheritance.
Advanced Inheritance Concepts
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Polymorphism and access control

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. DCS – SWC

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 DCS – SWC

What is polymorphism? Animal + makeSound() Duck Mouse + makeSound() DCS – SWC

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

What is polymorphism? public void makeAnimals() { ArrayList<Animal> zoo; zoo = new ArrayList<Animal>(); zoo.add(new Duck()); zoo.add(new Mouse()); makeNoise(zoo); } DCS – SWC

What is polymorphism? public void makeNoise(ArrayList<Animal> zoo) { for (Animal a : zoo) a.makeSound(); } Output: ”Quack” ”Squeak” DCS – SWC

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? DCS – SWC

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 DCS – SWC

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 DCS – SWC

Choosing methods Multiple versions of the same method can also be created 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 DCS – SWC

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 DCS – SWC

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

Abstract classes This is known as an abstract class 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 DCS – SWC

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 DCS – SWC

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 DCS – SWC

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 DCS – SWC

Preventing subclassing public class SecureAccount extends BankAccount { ... public final boolean checkPassword(String pwd) } public final class String {...} DCS – SWC

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

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 DCS – SWC

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… DCS – SWC

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? DCS – SWC

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 DCS – SWC

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

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… DCS – SWC

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 DCS – SWC

The Object class Deep copy Shallow copy Original Copy Org. Org. Copy DCS – SWC