Inheritance and Access Control CS 162 (Summer 2009)

20
Inheritance and Access Control CS 162 (Summer 2009)

Transcript of Inheritance and Access Control CS 162 (Summer 2009)

Page 1: Inheritance and Access Control CS 162 (Summer 2009)

Inheritance and Access Control

CS 162 (Summer 2009)

Page 2: Inheritance and Access Control CS 162 (Summer 2009)

Class Relationships

• Has-A (one class uses or is composed of others - composition)

• Implements-A (one class implements an interface shared among multiple others)

• Is-A (inheritance – a class to be implemented specializes a more general one

Page 3: Inheritance and Access Control CS 162 (Summer 2009)

Inheritance Terminology

• Child class, subclass, derived class• Parent class, superclass, [base class]• Base class – usually, a class with no parent

classes (the “root” of the hierarchy). Sometimes, this term just means parent class

Page 4: Inheritance and Access Control CS 162 (Summer 2009)

Access Control

• public data fields and methods are accessible to any derived class (as they are accessible to any class)

Page 5: Inheritance and Access Control CS 162 (Summer 2009)

Access Control

• private data fields and methods are not accessible to derived classes (although they are inherited)– In other words, a new method defined in a

derived class cannot make use of any private data fields or methods in the parent class.

– However, public methods inherited from the parent class still function correctly.

Page 6: Inheritance and Access Control CS 162 (Summer 2009)

Access Control

• protected data fields and methods can be accessed by the object’s class and all of its subclasses (+ all other classes in the same package) – what’s the point?– in other words, protected data is not really

protected at all

• default access control is ‘package’ – accessible to all classes in the same package

Page 7: Inheritance and Access Control CS 162 (Summer 2009)

Derived Classes

• Data Fields• Methods

Page 8: Inheritance and Access Control CS 162 (Summer 2009)

Data Fields

• All fields from the parent class are automatically inherited, even though they might not be visible.

• Derived classes can add new data fields that don’t exist in the base class (using any access control scheme)

• Parent class data fields cannot be overridden or removed – all fields from parent class are automatically inherited

Page 9: Inheritance and Access Control CS 162 (Summer 2009)

Methods

• All methods from the base class are automatically inherited, even though one might not be able to call them (reusability)

• Derived classes can override methods defined in the parent class (by supplying a different public implementation of a method that exists in the base class)

Page 10: Inheritance and Access Control CS 162 (Summer 2009)

Methods

• Derived classes can overload methods defined in the parent class (by supplying a method that exists in the parent class but has a different signature)

• Derived classes can add methods that don’t exist in the base class

Page 11: Inheritance and Access Control CS 162 (Summer 2009)

Overriding Methods

– Overriding method must have the same signature as the method in the parent class being overridden

– Objects instantiating the derived class will call the overriding method instead of the original one

– Can use ‘super’ in the overriding method to call a method from the parent class (like this).• eg, super.getBalance() calls getBalance() from the

parent class

Page 12: Inheritance and Access Control CS 162 (Summer 2009)

Overriding Methods

• Overriding can be prevented in derived classes with the ‘final’ keyword:– eg, public final void sendFlowersToMyLovelyWife();

Page 13: Inheritance and Access Control CS 162 (Summer 2009)

Inheritance Rules

• Derived classes cannot remove any data fields or methods from parent classes

• public methods in a parent class cannot be overridden by private methods in a derived class (ie, you cannot make a public method private)

• No multiple inheritance in Java (see: C++/Complicated)– Exception: multiple interface implementations

allowed

Page 14: Inheritance and Access Control CS 162 (Summer 2009)

Inheritance Rules

• Parent classes cannot inherit from derived classes (no loops)

• Every class automatically inherits from Object– gains a toString() and an equals() method (and

some others)

• Primitive types are not objects (wrappers required to do inheritance)

Page 15: Inheritance and Access Control CS 162 (Summer 2009)

Super

• Used to directly call methods in the parent class

• Also used to call the parent class constructor:public class DeathStarV2 extends DeathStar { public DeathStarV2(int numStormTroopers) { super(numStormTroopers); // calls DeathStar constructor improvedBlinkyLights = new BlinkyLights(); … }}

Page 16: Inheritance and Access Control CS 162 (Summer 2009)

Super

• super() must be the first line of your new constructor.

• If you don’t start your constructor with a call to super(something), then the default constructor is automatically called (the one with no arguments)

• If there is no such default constructor, you get an error

Page 17: Inheritance and Access Control CS 162 (Summer 2009)

Converting subclasses to superclasses

subclass -> superclass (widening – always okay)Square s = new Square();Rectangle r = s;Object o = r;

Page 18: Inheritance and Access Control CS 162 (Summer 2009)

Converting Super to Sub

– superclass -> subclass (narrowing – can be dicey)Square s = new Square();Object o = s;Rectangle r = (Rectangle) o; // cast operator• Casting is a run-time check – can generate an exception

if o is not actually a rectangle (as in this case)

Page 19: Inheritance and Access Control CS 162 (Summer 2009)

instanceof

if (o instanceof Rectangle) { Rectangle r = (Rectangle) o;}

• Every object is an instanceof its own class and each of its parent classes

Page 20: Inheritance and Access Control CS 162 (Summer 2009)

Late Binding

• Java looks at the actual object type whenever methods are called/dispatched

• eg,Rectangle r = new Square();float area = r.area(); // calls the area() method associated with the Square class