lecture29 - COMP 250 Winter 2010 29 - inheritance 1 March...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 250 Winter 2010 29 - inheritance 1 March 24, 2010 Inheritance In our daily lives, we classify the many things around us. We know that the world has objects like dogs and cars and food and we are familiar with talking about these objects as classes: Dogs are animals that have four legs and people have them as pets and they bark, etc. We also talk about specific objects (instances): When I was growing up I had a beagle named Buddy. Like most beagles, he loved to chase rabbits. We also talk about classes of objects at different levels. For example, take animals, dogs, and beagles. Beagles are dogs, and dogs are animals, and these is-a relationships between class are very important in how we talk about them. Buddy the beagle was a dog, and so he was also an animal. But certain things I might say about Buddy make more sense in thinking of him as an animal, than in thinking about him as a dog or as a beagle. For example, when I say that Buddy was born in 1966, this statement is tied to him being animal rather than him being a dog or a beagle. (Being born is something animals do in general, not something specific to dogs or beagles.) So being born is something that is part of the definition of a being an animal. Dogs automatically inherit this, since dogs are animals. Similarly, beagles automatically inherit it, since they are dogs and dogs are animals. A similar classification of objects is used in (object oriented) programming languages. In Java, for example, we can define new (sub)classes from existing classes. When we define a class in Java, we specify certain fields and methods. When we define a subclass, we may introduce entirely new fields and methods into the new class, or some of the fields or methods of the subclass may be given the same names as those of an existing class (but we change the body a method if we wish), or we can let the new class just inherit (unchanged) the fields or methods of the existing class. We will examine these choices over the next few lectures. Terminology If we have a class Dog and we define a new class Beagle which extends the class Dog , we would say that Dog is the base class or super class or parent class and Beagle is the subclass or derived class or extended class . We say that a subclass inherits the fields and methods of the superclass. class Dog { String name String owner int serialNumber Date birth Date death void Dog(){ .. } : : void bark(){ System.out.println("woof"); } } 1 COMP 250 Winter 2010 29 - inheritance 1 March 24, 2010 class Beagle extends Dog{ void bark(){ System.out.println("aaaaawwwwooooooo"); }} class Doberman extends Dog{ void bark(){ System.out.println("GRRRR! WO WO WO!"); }} class Terrier extends Dog{ void bark(){ System.out.println(" yap! yap! yap! "); }} When we declare the Beagle , Doberman , Terrier classes, we dont need to re-declare all the fields of the Dog class. These fields are automatically inherited, because of the keywordclass....
View Full Document

Page1 / 6

lecture29 - COMP 250 Winter 2010 29 - inheritance 1 March...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online