Download presentation
Presentation is loading. Please wait.
Published byCory Craig Modified over 9 years ago
1
Inheritance and Access Control CS 162 (Summer 2009)
2
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
3
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
4
Access Control public data fields and methods are accessible to any derived class (as they are accessible to any class)
5
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.
6
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
7
Derived Classes Data Fields Methods
8
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
9
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)
10
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
11
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
12
Overriding Methods Overriding can be prevented in derived classes with the ‘final’ keyword: – eg, public final void sendFlowersToMyLovelyWife();
13
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
14
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)
15
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(); … } }
16
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
17
Converting subclasses to superclasses subclass -> superclass (widening – always okay) Square s = new Square(); Rectangle r = s; Object o = r;
18
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)
19
instanceof if (o instanceof Rectangle) { Rectangle r = (Rectangle) o; } Every object is an instanceof its own class and each of its parent classes
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.