60 likes | 199 Vues
This tutorial covers key concepts of inheritance in object-oriented programming, focusing on superclasses and subclasses. Learn how classes can inherit fields and methods, creating a hierarchy of generic (superclass) and specialized (subclass) entities. Discover how constructors are chained during object creation and learn how to override methods to implement specialized behavior in subclasses. Practical examples, including method overriding in Java, will enhance your understanding. Ideal for those preparing for exams in computer science courses like CSC207H.
E N D
Tutorial 5 Superclasses, Subclasses and 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 Inheritance
Every time a new Object is created, constructors are chain-call until Object Class constructor is called! Inheritance and Constructors
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 Overriding Methods
http://exams.library.utoronto.ca.myaccess.library.utoronto.ca/browse?type=subject&order=ASC&rpp=20&value=CSC207Hhttp://exams.library.utoronto.ca.myaccess.library.utoronto.ca/browse?type=subject&order=ASC&rpp=20&value=CSC207H • Practice the questions that seem relevant! MIDTERM REVIEW
CommaSeparatedValues (CSV) • You are writing Parser/Evaluator • Syntax specified by CFG • OO design (CRC cards) will help you create a tree for evaluating recursively! A1