Chapter 3 Inheritance and Polymorphism

Ace your homework & exams now with Quizwiz!

Inheritance

Defines a relationship between objects that share characteristics. The mechanism by which a new class called a subclass is created from an existing class called a superclass, by absorbing its state and behavior and augmenting these with its own unique features. The subclass inherits characteristics of the superclass.

How can you override a public method in a subclass

Defining a method with the same return type and signature(name and parameter types.

Classes of the same level hierarchy

Do not inherit anything from each other

An abstract class can have both

instance variables and concrete(nonabstract) methods

The implements keyword

public class Bird implements FlyingObject{ } A class that extends a superclass can also directly implements an interface. The extends clause must precede must precede the implements clause. A class can have just one superclass but it can implements any number of interfaces.

Defining an interface

public interface FlyingObject{ void fly(); boolean isFlying(); }

Interface

A collection of realted methods either abstract or default (implementation provided in the itnerface). Default methods aren't on test. Non default are public and abstract- no need to specifically include these keywords. A class that implements an interface can define any number of methods. In particular, it contracts to provide implementations for all the non-default(ie abstract) methods. If it fails to implement any of the methods, the class must be declared abstract.

Polymorphism

A method that has been overidden in at least one subclass is said to be polymorphic. This is the mechanism of selecting the appropriate method for a particular object in a class hierarchy. The correct method is chosen bc in Java method calls are always determined by the type of actual object not the type of object reference bc in Java the selection of the method occurs during the actual run of the program.

Rules for subclasses

A subclass can add new priv instance variables, add new public, priv, or static methods, override inherited variables. A subclass can not redefine a public method as private or override static methods of the superclass. It should define own constructors and it cannot directly access the priv members of its superclass it must use accessors or mutator methods.

Using super in a subclass

A subclass can call a method in its superclass by using super. Suppose that the superclass method then calls another method that has been overriden in the subclass, by polymorphism the method that is executed is the one in the subclass. The computer keeps track and executes any pending statements any pending statements in either method.

Super

A subclass constructor can be implemented with a call to the super method which invokes the superclass constructor. super(); For each constructor the call to super has the effect of initializing the instance variables exactly as they are initialized in the superclass.

Abstract class

A superclass that represents an abstract concept and therefore should not be instantiated. For a maze program, it could have several components-paths, walls, exits. All these share certain features (like location and way of displaying). They can all therefore be declared as subclasses of the abstract class MazeComponent. The program will create path objects, wall objects and so on but no instances of MazeComponent. It also may contain abstract methods with no implementation code just a header. The rational is that there is no good default code for the method-every subclass will need to override it so why bother with a meaningless implementation in the superclass. It is a placeholder. If a class contains any abstract methods it must be declared an abstract class.

The abstract keyword

An abstract class is declared with the keyword abstract in the header. public abstract class AbstractClass { the keyword extends is used as before to declare a subclass public class SubClass extends AbstractClass { If a subclass of an abstract class does not provide implementation code for all the abstract methods of its superclass, it too becomes an abstract class and must be declared as such to aboid a compile error public abstract class SubClass extends AbstractClass.

Constructors

Can never be inherited. If no constructor is written for a subclass, the superclass default constructor with no parameters is generated if it doesn't have one just a constructor with parameters there will be a compiler error. If there is a default constructor in the superclass, inherited data members will be initialized as for the superclass and they will get a default initialization-0 for primitives and null for reference.

Downcasting

Casting a superclass to a subclass type is called downcast.

Super rule

If the super is used in the implementation of a subclass constructor it must be used in the first line of the constructor body.

Dynamic(late) binding

Making a run time decision about which instance method to call. This is different than selecting the method when they are overloaded rather than overridden. The compiler selects the correct overloaded method at compile time by comparing the method's signatures known as static binding or early binding. In polymorphism the compiler does not determine which method to call.

Can private methods be overridden?

No

Do subclasses do not inherit the private instance variables or private methods of their superclasses?

No but objects of subclasses contain memory for those private instance variables even though they can't directly accessed them.

A subclass inherits all the

Public and protected data members of its parent

Partial override

Sometimes the code for overriding a method includes a call to the superclass method. Occurs when the subclass method wants to do what the superclass does plus something extra. Achieved by using the keyword super in the implementation.

Implementing subclasses

Specified in the declaration of the subclass-using the keyword extends. THE extends KEYWORD

Is a subclass or superclass bigger?

The subclass is bigger-contains more data and more methods.

The ClassCastException

This is a run time exception thrown to signal an attempt to cast an object to a class of which it is not an instance.

Declaring Subclass objects

When a superclass object is declared in a client program, that reference can refer not only to an object of the superclass bu also to objects of any of its superclasses.

Does polymorphism work for abstract classes?

Yes

Can a subclass have additional methods and instance variables not in the superclass?

Yes and it may redefine a method it inherits-called method overriding. If part of the original method implementations is retained we call it partial overriding.

Type rules for polymorphic method calls

a.method(b) The method selected by type of a at run time. Parameter b must be of correct type at compile time. For a declaration like Superclass a = new Subclass(); the type of a at compile time is Superclass; at run time it is subclass. At compile time method must be found in the class of a, that is superclass(this is true whether the method is polymorphic or not.) If method cannot be found in the class of a, you do need to do an explicit cast on a to its actual type. For a polymorphic emthod, at run-time the actual type of a is determines-subclass in tis example-and method is selected from subclass. This could be an inherited method if there is no overriding method. The type of parameter b is checked at compile time. You may need to do an explicit cast to the subclass type to make this correct.


Related study sets

Chapter 7 Intermediate Accounting

View Set

Fluid and Electrolytes - Lewis based

View Set

Early Onset of Labor - ATI CH 10

View Set

Physics and Math: Light and Optics

View Set

Chapter 3: TREC and Licensing Requirements

View Set