Classes and Objects

8
Classes and Objects • Java programs are written as collections of classes, which serve as templates for individual objects. Each object is an instance of a particular class, which can serve as a pattern for many different objects. Classes in Java form hierarchies. Except for the class named Object that stands at the top of the hierarchy, every class in Java is a subclass of some other class, which is called its superclass. A class can have many subclasses, but each class has only one superclass. • A class represents a specialization of its superclass. If you create an object that is an instance of a class, that object is also an instance of all other classes in the hierarchy above it in the superclass chain.

description

Classes and Objects. Java programs are written as collections of classes, which serve as templates for individual objects. Each object is an instance of a particular class, which can serve as a pattern for many different objects. - PowerPoint PPT Presentation

Transcript of Classes and Objects

Page 1: Classes and Objects

Classes and Objects• Java programs are written as collections of classes, which

serve as templates for individual objects. Each object is an instance of a particular class, which can serve as a pattern for many different objects.

• Classes in Java form hierarchies. Except for the class named Object that stands at the top of the hierarchy, every class in Java is a subclass of some other class, which is called its superclass. A class can have many subclasses, but each class has only one superclass.

• A class represents a specialization of its superclass. If you create an object that is an instance of a class, that object is also an instance of all other classes in the hierarchy above it in the superclass chain.

• When you define a new class in Java, that class automatically inherits the behavior of its superclass.

Page 2: Classes and Objects

Biological Models of Class StructureThe structure of Java’s class hierarchy resembles the biological classification scheme introduced by Scandinavian botanist Carl Linnaeus in the 18th century. Linnaeus’s contribution was to recognize that organisms fit into a hierarchical classification scheme in which the placement of individual species reflects anatomical similarities.

Carl Linnaeus (1707–1778)

Page 3: Classes and Objects

Biological Class HierarchyLiving Things

Plants Animals Fungi

Annelida Brachiopoda Arthropoda Mollusca Chordata

Crustacea Insecta Arachnida

Hymenoptera

Formicidae

Iridomyrmex

purpureus

Kingdom

Phylum

Order

Class

Family

Genus

Species

Classification of the red ant Iridomyrmex purpureus

Every red ant is also an animal, an arthropod, and an insect, as well as the other superclasses in the chain.

Note that there can be many individual red ants, each of which is an instance of the same basic class.

Page 4: Classes and Objects

The Fruit HierarchyThe classes that represent fruit objects form a hierarchy, part of which looks like this:

Fruit

Banana Orange PineAppleApple

The Fruit class represents the collection of all fruit objects. The four subclasses shown in this diagram correspond to particular types of objects: apples, bananas, oranges, pine apples. The class diagram makes it clear that any Apple, Banana, Orange, or PineApple is also a Fruit.

Page 5: Classes and Objects

The Animal HierarchyThe classes that represent animal objects form a hierarchy, part of which looks like this:

Animal

Dog Crow HorseCat

The Animal class represents all animal objects. The four subclasses shown in this diagram correspond to particular types of objects: cats, dogs, crows, horses. The class diagram makes it clear that any Cat, Dog, Crow, or Horse is also an Animal. But the inverse is NOT true. That is, any Animal is not a Cat; any Animal is NOT a Dog, etc.

Page 6: Classes and Objects

InheritanceInheritance relation in Java is specified using the extends keyword.

public class Cat extends Animal { ...

public class Dog extends Animal { ...

Page 7: Classes and Objects

InheritanceIf no superclass is defined, by default, the class will inherit from the Object class.

public class Animal { ...

public class Animal extends Object { ...

is the same as

Animal

Dog Crow HorseCat

That is, the hierarchy really is Object

Page 8: Classes and Objects

InheritanceA subclass may redefine a method that is defined by a superclass. In this case, it is said that the subclass overrides the method.