inheritance and polymorphism- i math 130 introduction to computer programming lecture #27 monday,...
TRANSCRIPT
Inheritance and Polymorphism- IInheritance and Polymorphism- I
Math 130Introduction to Computer Programming
Lecture #27
Monday, November 5, 2007
Learning Outcomes
• Write programs that are easily extensible and modifiable by applying polymorphism in program design
• Define reusable classes based on inheritance and abstract classes and abstract methods
• Differentiate the abstract classes and Java interface
• Define methods, using the protected modifier
What is Inheritance?Generalization vs. Specialization
• Real-life things are typically specialized versions of other more general objects.
• The term “insect” describes a very general type of creature with numerous characteristics.
• grasshoppers and bumblebees are insects• they share the general characteristics of an insect.• However, they have special characteristics of their
own.• grasshoppers have a jumping ability, and• bumblebees have a stinger.
• Grasshoppers and bumblebees are specialized versions of an insect.
Inheritance
Insect
GrasshopperBumbleBee
Contains those attributes and methods that are shared by all insects.
Contains those attributes and methods that are specific to a
Bumble Bee.
Contains those attributes and methods that are specific to a
Grasshopper.
Inheritance and Polymorphism 5
Exampleclass Pet { private String name;
public String getName() { return name; }
public void setName(String petName) { name = petName; }
public String speak() { return “I’m your cuddly pet.”; }}
Pet myPet = new Pet();
System.out.println(myPet.speak());
Inheritance and Polymorphism 6
class Cat extends Pet{
public String speak() { return “Don’t give me orders.\n” + “I speak only when I want to.”; }}
Cat myCat = new Cat();
myCat.setName(“Cha Cha”);
System.out.println(Hi, my name is “ + myCat.getName());
Cat myCat = new Cat();
myCat.setName(“Puff Puff”);
System.out.println(myCat.getName( ) + ” says: “);
System.out.println(myCat.speak( ) );
Inheritance and Polymorphism 7
class Dog extends Pet{
public String fetch() { return “Yes, master. Fetch I will.”; }}
Inheritance and Polymorphism 8
class Pet { private String name;
public String getName() { return name; }
public void setName(String petName) { name = petName; }
public String speak() { return “I’m your cuddly pet.”; }}
class Cat extends Pet{
public String speak() { return “Don’t give me orders.\n” + “I speak only when I want to.”; }}
class Dog extends Pet{
public String fetch() { return “Yes, master. Fetch I will.”; }}
Pet p;
p = new Dog( );
System.out.println( p.speak() );
p = new Cat( );
System.out.println( p.speak() );
Pet p;
p = new Dog( );
// invalid
System.out.println( p.fetch() );
p = new Dog( );
System.out.println( (Dog)p.fetch() );
Inheritance and Polymorphism 9
Which is valid?
1) Pet p = new Cat( );
2) Cat c = new Pet();
Inheritance and Polymorphism 10
Is the following code valid?
Pet p = new Dog();System.out.println( p.fetch() );
The “is a” Relationship
• The relationship between a base class and an inherited class is called an “is a” relationship.
• A Grasshopper “is a” insect.• A poodle “is a” dog.• A car “is a” vehicle.
• A specialized object has:• all of the characteristics of the general object,
plus• additional characteristics that make it special.
• In object-oriented programming, inheritance is used to create an “is a” relationship among classes.
The “is a” Relationship
• We can extend the capabilities of a class.
• Inheritance involves a base class and a derived class.• The base class is the general class and• the derived class is the specialized class.
• The derived class is based on, or derived from, the base class.• Base classes are more commonly called super classes,
and• derived classes are more commonly called sub classes.
• The relationship of classes can be thought of as parent classes and child classes.
Inheritance
• The derived class inherits fields and methods from the base class without any of them being rewritten.
• New fields and methods may be added to the derived class.
• The Java keyword, extends, is used on the class header to define the base class.public class FinalExam extends GradedActivity{…}
The GradedActivity Example
• Example:
• GradedActivity.java,• GradeDemo.java• FinalExam.java, • FinalExamDemo.java
GradedActivity
- score : double
+ setScore(s : double) : void+ getScore() : double+ getGrade() : char
FinalExam
- numQuestions : int- pointsEach : double- numMissed : int
+ FinalExam(questions : int, missed : int) :+ getPointsEach() : double+ getNumMissed() : int
Contains those attributes and methods that are shared by all graded activities.
Contains those attributes and methods that are specific to the FinalExam class.Inherits all non-private attributes and
methods from the GradedActivity class.
Inheritance, Fields and Methods
• Members of the super class that are marked private:
• are not inherited by the sub class, and• may only be accessed from the sub class by
public methods of the super class.
• Members of the super class that are marked public:
• are inherited by the sub class, and• may be directly accessed from the sub class.
Inheritance, Fields and Methods
• When an instance of the sub class is created, the non-private methods of the super class are available through the sub class object.FinalExam exam = new FinalExam();
exam.setScore(85.0);
System.out.println(“Score = “ + exam.getScore());
• Non-private methods and fields of the super class are available in the sub class.setScore(newScore);
Inheritance and Constructors
• Constructors are not inherited.
• When a derived class is instantiated, the base class default constructor is executed first.
• Example:
• SuperClass1.java,
• SubClass1.java
• ConstructorDemo1.java
B Smith:
Start here wed with sec01. Rate: 3 so far. 50 min.
Diversion: classpath, code
reuse, library creation
B Smith:
Start here wed with sec01. Rate: 3 so far. 50 min.
Diversion: classpath, code
reuse, library creation
BlueJ Example
what happens when we change from “abstract” to “public”?