Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Inheritance and Event Handling in Object-Oriented Programming - Prof. Kenrick J. Mock, Study notes of Computer Science

An introduction to inheritance and event handling in object-oriented programming (oop). Inheritance is a technique used to create a new class from an existing one, allowing code reuse and specialization. Event handling is a way to get user input and react to specific events, such as mouse clicks or key presses.

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-2qe
koofers-user-2qe 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
11/29/2009
1
Alice
Inheritance and Event Handling
Inheritance Concept
Consider this hierarchy; parents describe
properties of children
Animals
Vertebrates
MammalsFish
Carnivores
BearsCatsDogs
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Inheritance and Event Handling in Object-Oriented Programming - Prof. Kenrick J. Mock and more Study notes Computer Science in PDF only on Docsity!

Alice

Inheritance and Event Handling

Inheritance Concept

  • Consider this hierarchy; parents describe

properties of children

Animals

Vertebrates

Fish Mammals

Carnivores

Dogs Cats Bears

Introduction to Inheritance

  • Inheritance is one of the main techniques of object- oriented programming (OOP)
  • Using this technique, a very general form of a class is first defined and compiled, and then more specialized versions of the class are defined by adding properties and methods - The specialized classes are said to inherit the methods and properties of the general class

Introduction to Inheritance

  • Inheritance is the process by which a new class is created from another class - The new class is called a derived class - The original class is called the base class
  • A derived class automatically has all the properties and methods that the base class has, and it can have additional methods and/or properties as well
  • Inheritance is especially advantageous because it allows code to be reused , without having to copy it into the definitions of the derived classes

A Class Hierarchy

Inheritance in Alice

  • Alice supports a rather weak form of inheritance; a more powerful/complex version exists in most OOP languages like Java, C++, or C# - Add existing class to the project - Create new methods for the class - Save out as a new class using a different name - Can now Import the new class and it has the properties/methods of the parent along with the new methods - TO DO: Demonstrate creating and saving new class
  • The process is more dynamic and seamless in other OOP languages

Guidelines for Class-Level Methods

  • Create many different class level methods and study methods already written for some objects (e.g. see animal classes)
  • Play a sound in a class level method only if the sound has been imported for the object, instead of the world - Not loaded if imported into another world
  • Do not call world level methods from class level methods
  • Do not use instructions for other objects from within a class level method - Possible exceptions include camera, world objects

Interaction: Events and Event Handling

  • To date, most of our programs have been movie-

centric

  • Many programs are user-centric, requiring user

input

  • One way to get user input is through events
  • An event is when something happens, like a

particular condition is met, the user clicks the

mouse, hits a key, or some other action is

performed

  • We can write methods that react to events, such methods are called event handlers

Example

To Do:

  • Add another event that completes a barrel roll

when the spacebar is pressed

  • Simulate a plane crash into the building
    • Create a new Biplane variable under its Properties named “Flying” and set to True
    • While true, the plane is flying, if false, it has crashed

Plane Crash

  • In the world method, the plane should only go

forward if it is flying

  • Make a new biplane method, Crashed, that

moves the plane to the ground and sets flying

to false

Plane Crash

  • Add event : while some condition, do event

handler. In this case, crash if the plane is close

enough to the building

LightningZap Method

Can test by invoking it from the Main method

Using LightningZap in a Click Event

  • Create a new click event for whatever the

mouse is under

  • Send in the object clicked as the parameter for

LightningZap

Class Exercise

  • Penguin Slide
    • Create a world with a lake scene and three penguins. Allow the user to click on a penguin and make it slide down the slope into the water then disappear. Add a parameter to control how many times the penguin spins as it slides.
    • Hint: Use an invisible object as the target