tutorial 5 superclasses, subclasses and inheritance

6
Tutorial 5 Superclasses, Subclasses and Inheritance

Upload: giles-robinson

Post on 30-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Tutorial 5 Superclasses, Subclasses and Inheritance

Tutorial 5Superclasses, Subclasses and Inheritance

Page 2: Tutorial 5 Superclasses, Subclasses and Inheritance

Inheritance

Classes can be derived from other classes, thereby inheriting fields and methods from those classes

Subclass - derived class (Specialised)Superclass – inherited class (Generic)Basic Idea: Inherit everything from

Superclass and extend its abilities

Page 3: Tutorial 5 Superclasses, Subclasses and Inheritance

Inheritance and Constructors

Every time a new Object is created, constructors are chain-call until Object Class constructor is called!

Page 4: Tutorial 5 Superclasses, Subclasses and Inheritance

Overriding Methods

Overriding:  Subclass modifies the implementation of a method defined in the Superclass

Car c2 = new Porsche();c2.makeSound(); //”Porsche vroommm!

Java always uses methods and variables of the more “specialised” class

Page 6: Tutorial 5 Superclasses, Subclasses and Inheritance

A1CommaSeparatedValues (CSV)

You are writing Parser/Evaluator

Syntax specified by CFG

OO design (CRC cards) will help you create a tree for evaluating recursively!