The document discusses object-oriented programming concepts like inheritance, derived classes, overriding methods, polymorphism, upcasting and downcasting. It explains how derived classes inherit from base classes and can add additional methods and properties. Methods can be overridden in derived classes and final methods cannot be overridden. Polymorphism allows methods to have different implementations depending on the object type at runtime.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views15 pages
Inheritance+ +University+of+Denver
The document discusses object-oriented programming concepts like inheritance, derived classes, overriding methods, polymorphism, upcasting and downcasting. It explains how derived classes inherit from base classes and can add additional methods and properties. Methods can be overridden in derived classes and final methods cannot be overridden. Polymorphism allows methods to have different implementations depending on the object type at runtime.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15
Inheritance
(Chapter 7)
● 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 instance variables and methods ● The specialized classes are said to inherit the methods and instance variables of the general class ● Example: – General class: Animal – Classes derived from Animal: Bird, Mammal, Fish...etc – Classes derived from Mammal: Cat, Dog, Bear...etc Inheritance (cont.) ● 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 instance variables and methods that the base class has, and it can have additional methods and/or instance variables 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 Derived Classes ● When designing certain classes, there is often a natural hierarchy for grouping them ● Animals can be classified into Amphibians, Mammals, Reptiles ● Amphibians can be broken into: Frogs, Salamander..etc ● Mammals can be broken into: Dog, Cat, Bear..etc ● Reptiles can be broken into: Snake, Turtle..etc. Derived Classes ● All Animals share things in common: ● All Animals have a weight, age and are born and die. ● The methods for setting weight, age and changing status to dead are the same for all animals ● Some Animals have specialized characteristics ● Mammals give birth while Amphibians lay eggs. ● The birth methods for the two derived classes would be different. Deriving From Base Class ● Since a Mammal is an Animal, it is defined as a derived class of the class Animal ● A derived class is defined by adding instance variables and methods to an existing class ● The existing class that the derived class is built upon is called the base class ● The phrase extends BaseClass must be added to the derived class definition: public class Mammal extends Animal Overriding a Method Definition ● Although a derived class inherits methods from the base class, it can change or override an inherited method if necessary ● In order to override a method definition, a new definition of the method is simply placed in the class definition, just like any other method that is added to the derived class ● You can invoke the parent class method ONLY from inside the overriding method using super. Example: public String toString() { return (super.toString() + “ Hair type: “ + myHairType); } Changing the Access Permission of an Overridden Method ● The access permission of an overridden method can be changed from private in the base class to public (or some other more permissive access) in the derived class ● However, the access permission of an overridden method can not be changed from public in the base class to a more restricted access permission in the derived class The final Modifier ● If the modifier final is placed before the definition of a method, then that method may not be redefined in a derived class ● It the modifier final is placed before the definition of a class, then that class may not be used as a base class to derive other classes The super Constructor ● A derived class uses a constructor from the base class to initialize all the data inherited from the base class ● In order to invoke a constructor from the base class, it uses a special syntax: public Mammal(String nameValue, int ageValue, String hairTypeValue) { super(nameValue, ageValue); MyHairType = hairTypeValue; } • In the above example, super(nameValue, ageValue); is a call to the base class constructor: public Animal(String n, int a){ … } The super Constructor ● A call to the base class constructor can never use the name of the base class, but uses the keyword super instead. ● A call to super must always be the first action taken in a constructor definition ● An instance variable cannot be used as an argument to super The Class named Object ● The Object class is an ancestor of every class in Java. ● When you write toString, clone and equals methods, you are overriding them inside class Object. Polymorphism (Chapter 8, P.474-487) ● Polymorphism is assigning multiple meaning to the same method name using late binding ● Late binding: we will not bind the method calling with the method definition until run time. ● Each object will know which method it should be using at run time ● If you don't want a method to have different meaning (you don't want it to be overridden), then you use the final modifier public final void methodName() ● If the class is defined using the final modifier, then it cannot be a base class. No inheritance allowed public final class ClassName { Polymorphism (cont.) ● No late binding with: ● Private methods ● Static methods ● Final methods Upcasting and Downcasting ● Upcasting is when you assign an object to a variable of an ancestor type. ● Example: Animal a = new Cat(); ← This is always allowed since Cat class is derived from Animal ● Downcasting is when you cast an object of type ancestor to a derived variable type ● Example: Animal a = new Animal(); Cat c = (Cat) a; ← This doesn't work since an Animal doesn't have a myHairType parameter ● Downcasting above will generate a run-time error if the casting is not valid. ● It's up to the programmer to make sure the downcasting is meaningful. Abstract Classes ● You create an abstract class using the keyword abstract Example: public abstract class Shape{ … } ● You cannot create objects of type Shape since Shape is now abstract. ● You can still use Shape variable typ to store Circles, Squares...etc ● You use the abstract keyword to create abstract methods public abstract void drawMe();