36
Inheritance Encapsulation John Ross Wallrabenstein Slides based on work by Dr. Keith Frikken

Inheritance Encapsulation - Purdue University

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

InheritanceEncapsulation

John Ross Wallrabenstein

Slides based on work by Dr. Keith Frikken

Project 1

• How did it go?

• What did you like about it?

• What did you not like?

• What can we do to help?

Suggestions

• Ask questions if you don’t understand a concept

• Start Early!* (early != (dueDate-2))

• Come to Office Hours

• Come to Consulting Hours

• Ask Questions!!!

*This is the secret to success in this course!

What do you know already?

Inheritance

Basics

• Inheritance is an OOP concept that allows a class to inherit the instance variables and methods of another class.

• We say that a derived class that extends a base class inherits the instance variables and methods of the base class.

• The derived class can override methods from the base class to suit its purpose.

Terminology

• {Base, Parent, Ancestor, Super} Class - the more generalized class representing a large set of objects

• {Derived, Child, Descendant, Sub} Class - the more specific class representing a smaller set of objects

• All are correct - I will use Base and Derived

Inheritance Skeleton

• public class derived extends base {

//Instance Vars for derived class //Omit I.V.‘s from base class

//Overridden & New Methods //Omit Identical Methods from base

}

Cryptography Example

+ encrypt(BigInteger e) : BigInteger+ decrypt(BigInteger d) : BigInteger+ getParams(void) : ArrayList<BigInteger>+ setParams(ArrayList<BigInteger> p) : void+ generateKeys() : void

- input : BigInteger- output : BigInteger- params : ArrayList<BigInteger>

Encryption

Override Base Class Methods

- input : Object- output : Object

Paillier

Override Base Class Methods

- input : Object- output : Object

Goldwasser-Micali

Override Base Class Methods

- input : Object- output : Object

ElGamal

+ encrypt(BigInteger e) : BigInteger+ decrypt(BigInteger d) : BigInteger+ Cryptography(String type) : <constructor>

- type : StringCryptography

In-Class Example

• Now let’s modify a “naïve” design for software that models animals

• What variables and methods do you think the class Animal should have?

Animal Classpublic class Animal{

private int age; private String color; private boolean isEvil;

public int getAge(){ return age; } public String getColor(){ return color; } public boolean getEvilness(){ return isEvil; } public void makeNoise(){}

}

Animal Classpublic class Animal{

private int age; private String color; private boolean isEvil;

public int getAge(){ return age; } public String getColor(){ return color; } public boolean getEvilness(){ return isEvil; } public void makeNoise(){}

}

Naïve Cat Classpublic class Cat{

private int age; private String color; private boolean isEvil;

public int getAge(){ return age; } public String getColor(){ return color; } public boolean getEvilness(){ return true; } public void makeNoise(){ System.out.speak(“Meow”); }}

Do we really needall of this?

Help Me Re-Write the Cat Class

public class Animal{

private int age; private String color; private boolean isEvil;

public int getAge(){ return age; } public String getColor(){ return color; } public boolean getEvilness(){ return isEvil; } public void makeNoise(){}

}

Improvedpublic class Cat extends Animal{

//Duplicate Instance Variables Omitted

//Duplicate Methods Omitted

public boolean getEvilness(){ return true; }

//Override Necessary Methods public void makeNoise(){ System.out.speak(“Meow”); }}

This should really be omitted

...But cats really are evil

Copy Constructors• Bad

public Cat(Cat other){ setColor(other.color); setAge(other.age); setEvilness(other.isEvil); }

• Good*public Cat(Cat other){ if(other != null){ setColor(other.getColor()); setAge(other.getAge()); setEvilness(other.getEvilness()); }}

*Anything that makes more cats is inherently bad

The Object Class

• You have been extending classes before you were aware that you were

• If a base class is not explicitly extended, then the class Object is the base

• How does this affect the equals method?

equals

• We have actually been overloading the equals method, not overriding it

• Consider:

• Object Class equals equals(Object otherObject){...}

• Cat Class equals equals(Cat otherCat){...}

When is this a Problem?

• What about this?

Cat myCat = new Cat();Cat myCatTwin = new Cat(myCat);myCat.equals(myCatTwin) //trueObject myObject = myCatTwin;myCat.equals(myObject) //false

Solution: Take I

public boolean equals(Object other){ if(other == null) return false; if(! (other instanceof Cat)) return false; ...... Remainder of Equals Method

}

Problem

Cat myCat = new Cat(“Fluffy”,”Black”,5);Animal a = new Animal(“Fluffy”,”Black”,5);myCat.equals(a) //falsea.equals(myCat) //true

Our equals method is not symmetric!

Solution: Take II• Use getClass() - a method of Object, so it

is common to all classes

public boolean equals(Object o){ if(o == null) return false; if(! (o.getClass().equals( getClass() )) return false; Cat c = (Cat) o;

... Remainder of equals method

Using SUPER

SUPER

• With Java 1.5, the developers have acknowledged the fact that programmers are overwhelmed by infinite loops, null pointer exceptions, and array index out of bounds exceptions

• Whenever you encounter one of these scenario’s, simply call the super() method and Java will fix it for you*!

*We didn’t tell you until now so that you would appreciate SuperDuke!

Ok, ...maybe not

The super keyword

• Sometimes, we may wish to call methods from the base, or superclass, from the derived class

• This is done with the command super

• Ex: super.baseClassMethod( ... )

• Note: super.super is illegal

Constructors• A derived class should always call super() as

the first call in the constructor

• This makes the call explicit and lets the base class set up instance variables

• You can call either super or this as the first call in the constructor

• BUT NOT BOTH! (Exclusive-Or)

• What does the this keyword mean?

Calling other Base Class Methods

• You can call methods in the base class to avoid re-writing code in derived classes

• This is especially useful for equals methods

• Call the super.equals(Object) method, then add more specific constraints in the derived class

Encapsulation

Encapsulation

• What do you already know?

• What is the purpose of encapsulation?

• What keywords pertaining to encapsulation are you familiar with?

Package Access

• You have likely seen public, protected, and private access

• Package Access - allows the class itself, as well as classes in the same package access

• Is this more or less strict than protected?

Encapsulation Summary

Slide Author: Dr. Keith Frikken, Miami University

Public Protected Package Private

Class Itself Yes Yes Yes Yes

Same Package

Yes Yes Yes No

Derived Class

Yes Yes No No

Other Classes

Yes No No No

On Overriding Methods

• Heuristic: Any overriding method should be usable in any situation (possibly more) that the original could be used in

Questions

Quiz Time