githubEdit

Week6

[!TIP] ๐Ÿ”” ๆธฉ้ฆจๆ็คบ๏ผšๆœฌๅ‘จ็ปƒไน ไปฃ็ ๅœจ ../src/Week6 ไธญ ๐Ÿ”— ็‚นๆˆ‘่ทณ่ฝฌarrow-up-right

Introduction to Inheritance and "Is a" Relationship

review๏ผšencapsulationใ€data hiding Association relationship๏ผšhas

Inheritance

  • A superclass is a more generalized class

  • A subclass a more specialized class

GradedActivity

- score : double

โ†‘ | |

FinalExam

- numQuestions : int - pointsEach : double - numMissed : int

public class Car extends Vehicle {

}

[!NOTE] Inheritance can only go up

image

Subclasses and Superclasses

  • The more general class is known as the superclass Entity

  • The more specialized class is known as the subclass Hobbit

  • An instance of the subclass can be used whenever an instance of the superclass is required, e.g.

[!NOTE] A subclass in turn can have its own subclass, and so on.

The extends keyword

  • The relationship between a superclass and subclass is declared with the use of the extends keyword.

  • A class may have one, many or no subclasses.

  • A class may have only one superclass.

  • No multiple inheritance!

  • The this keyword

[!TIP] Remember: within a class, the keyword this is a reference to the instance of the class and allows us to refer to the attribute of the class rather than the passed-in parameter.

Inheritance and constructors

  • The subclass requires its own constructor.

  • This constructor will always make a call to a constructor of its superclass.

  • You can make this call explicit using the keyword super.

  • If you do not call the superclass constructor explicitly then Java will try to call a superclass constructor that takes no arguments โ€“ if it canโ€™t find one then it will report an error.

[!NOTE] If we donโ€™t write a constructor for the class, Java will create one for us. It will take no parameters, and assign a null value to name and symbol. Note that Java only does this if we havenโ€™t written a constructor ourselves.

[!NOTE] We do not write the superclass constructor. However, the superclass has a constructor taking no parameters. Java will call it for us. Note that if there wasnโ€™t a superclass constructor with no parameters then Java would report an error.

Inheritance and overriding

  • The subclass can also have methods that override methods of the superclass, with the same name & parameters

  • Overriding is an example of polymorphism. Thatโ€™s were you have two methods of the same name that behave differently.

use keyword @Override

Access Control

A method or attribute declared public will be available to/modifiable by all subclasses. A method or attribute declared private is not modifiable or directly accessible by subclasses. In that case public "getter" and "setter" methods like public int getX() provide access.

Access Specifiers

Access Modifier
Accessible to a subclass inside the same package?
Accessible to all other classes inside the same package?
Accessible to a subclass outside the package?
Accessible to all other classes outside the package?

default (no modifier)

โœ“

โœ“

ร—

ร—

Public

โœ“

โœ“

โœ“

โœ“

Protected

โœ“

โœ“

โœ“

ร—

Private

ร—

ร—

ร—

ร—

Overriding move

Overriding toString

Polymorphism

The method called depends on which class an object is an instance of, e.g.

will result in the method from the Hobbit class being called when

is called, because entity is an Entity object.

Polymorphism is an ability to change what a method does depending on the type of object. Both overloading and overriding are examples of polymorphism.

Overriding Superclass Methods

  • Recall that a methodโ€™s signature consists of:

    • the methodโ€™s name

    • the data types of the methodโ€™s parameters in the order that they appear.

  • A subclass method that overrides a superclass method must have the same signature as the superclass method.

  • An object of the subclass invokes the subclassโ€™s version of the method, not the superclassโ€™s.

Overriding and overloading Methods

  • There is a distinction between overloading a method and overriding a method.

  • Overloading is when a method has the same name as one or more other methods, but with a different signature.

  • When a method overrides another method, however, they both have the same signature. Which one is used is determined by the object that invokes that method.

  • Work on exercise 6

Object

Object is the supersupersuper... class of every single class. Everything extends Object (though you donโ€™t have to write that explicitly!) The Object class provides various useful methods to every class, which include boolean equals(Object obj) Indicates whether some other object is "equal to" this one.

String toString() return a string with information about this object.

Since every class extends Object we can override these methods.

See also the Java API documentation: Overview (Java Platform SE 6)arrow-up-right

Chains of Inheritance

  • A superclass can also be derived from another class.

image

Abstract classes

  • Sometimes we do not want to create direct instances of a superclass. For example we might want to insist that all instances of the Entity class are instances of some particular subclass of Entity โ€“ for example a Wizard or Hobbit. We can achieve this by making the class abstract. We do this by putting the word abstract in its declaration.

  • A class that is not abstract is said to be concrete.(Assume class Hobbit is a subclass of class Entity)

Abstract methods

  • An abstract class may have abstract methods.

  • An abstract method is declared, but not implemented.

Abstract classes in UML version 1

  • You can indicate that a class or method is abstract by writing its name in italics.

image

Uses of inheritance

  • If classes are arranged into inheritance then we can write methods that will work for any subclass of a given superclass.

Casting

You can cast objects of one type into another, so long as the types are compatible. e.g. if Wizard and Hobbit are subclasses of Entity

Uses of inheritance

  • If classes are arranged into inheritance then we create lists whose elements must be subclasses of a given superclass, but which can be of any subclass.

[!TIP] We can add objects of any subclass of Entity to the list. But we canโ€™t add any object that is not an instance of some Entity subclass.

Why use Inheritance?

  • Avoid writing shared code more than once

    • Useful for the lazy programmerโ€™s toolbox

    • Reduces the opportunities for error.

  • Useful for helping to structure programs

    • There are other features of object-oriented programming languages that help with this too

  • Make explicit the relationships between different types of objects

  • Can extend library classes with extra features

Last updated