Inheritance - Steve Houseman Page 1 Inheritance 10/5/2011...

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

View Full Document Right Arrow Icon
Steve Houseman Page 1 10/5/2011 Inheritance Object-oriented programming allows you to derive new classes from existing classes. This is called inheritance . Inheritance is an important and powerful concept in Java. In fact, every class you define in Java is inherited from an existing class, either explicitly or implicitly. Some of classes we have created previously were derived implicitly from the java.lang.object class. The primary feature of inheritance is a form of software reuse, in which a new class is created by absorbing an existing class’s members and embellishing them with new or modified capabilities. With inheritance, programmers save time during software development by reusing proven and debugged high-quality software. This also increases the likelihood that a system will be implemented effectively. Superclass and Subclass: Inheritance involves a superclass and a subclass. The superclass we create is a general class that defines traits common to a set of related items. This class can then be inherited (extended) by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass (also known as the base class) .The class that does the inheriting is called a subclass , which is a specialized class (also known as derived class). Therefore, subclass is a specialized version of superclass. It inherits all of the instance variables and methods defined by a superclass and adds it own, unique elements. Inheritance “Is a” Relationship: When an object of a subclass is a specialized version of a general object of the superclass, there is an “ is a relationship between them. That means that a specialized object has all the characteristics of the general object, plus additional characteristics that makes it special. In object-oriented programming, inheritance is used to create “is a” relationship among classes. This allows us to extend the capabilities of a class by creating another class that is a specialized version of it. A good place to begin investigating inheritance is with the real-world concept of a specialization. Automobile is a special case of transportation vehicle. Shirt is a particular kind of clothing . Bald eagle is a species of bird . Triangle is a type of polygon . This sort of relation between classes is known as is a relation , because one class is a specialized version of the other. An “ is a relation is an obvious situation for the use of inheritance. Inheritance Basics: To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. Definition of Subclass:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Steve Houseman Page 2 10/5/2011 class subclass name extends superclass name { declarations of additional instance variables definitions of additional methods } Let us look at a simple example how inheritance can be use. The following program creates a general superclass A and subclass B . Notice how the keyword
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

Inheritance - Steve Houseman Page 1 Inheritance 10/5/2011...

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