Chapter 7- Inheritance.

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

Introduction to Java 2 Programming
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Chapter 71 Inheritance Chapter 7. 2 Reminders Project 4 was due last night Project 5 released: due Oct 10:30 pm Project 2 regrades due by midnight.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Intro to OOP with Java, C. Thomas Wu
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Inheritance in Java.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Polymorphism 11-Nov-18.
Inheritance, Polymorphism, and Interfaces
Inheritance Chapter 7 Chapter 7.
Inheritance Basics Programming with Inheritance
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Comp 249 Programming Methodology
Inheritance, Polymorphism, and Interfaces. Oh My
Learning Objectives Inheritance Virtual Function.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
More About Inheritance & Interfaces
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Topic 31 - inheritance.
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Chapter 7- Inheritance

Overview What is inheritance? How to inherit from other classes Overloading and overriding Inheritance details Review

What is inheritance?

Inheritance defined- The birds and the bees. Inheritance is the process of automatically taking on properties of an ancestor: You inherited genes from your parents. Organisms inherit their species from ancestors. Classes can also have inheritance.

Class inheritance. When class B inherits characteristics from class A, class B receives all the variables and public methods that class A had. Class B can still have more methods and variables, but it has all of A’s stuff as bonus. Class B can also modify the methods it receives from A to better suit its needs.

Class inheritance. The ancestors are often called “parents” and the classes that receive the inheritance are called “children.” Also sometimes called “base class” and “derived class”, respectively. Usually inheritance of classes creates a kind of family tree that goes from a general class down to a more specific class that is actually useful. There can be multiple levels in this tree.

Class Inheritance. Parent Child Child

Class Inheritance example Shape Polygon Squiggly Quadri- lateral Triangle Rectangle Square Rhombus

Class Inheritance Example: Person Student Employee Under- graduate Graduate Faculty Staff Masters PhD NonDegree

How to inherit from other classes.

How to inherit. To have class A inherit from class B, we only have to add two words to the class declaration of A: “extends B” public class Student extends Person {…} public class Square extends Rectangle public class Triangle extends Polygon

What does extending do? When we inherit from class B, we take into our class all of the following: B’s variables Public variables of B Private variables of B B’s methods Only the public methods of B.

What does that mean? Say that Shape had a private variable called color and public methods setColor and getColor. Then Triangle, Rectangle, Square, and any other class that inherits from Shape also have a private variable called color and public methods setColor and getColor. If Person had a private name variable, then all of the Students and Employees would automatically have a name variable.

A trick to get access to private variables? We can’t use inheritance to give us access to another class’s private variables. In the previous examples, color was private, so Rectangle would have inherited the color variable, but it would not have direct access to the variable. It would have to go through the public methods getColor and setColor. We can have direct access to public variables though.

Private variable example. … Rectangle a = new Rectangle(); a.color = “Blue”; //ILLEGAL a.setColor(“Blue”); //Legal. a.getColor(); //Legal, would return blue a.color; //ILLEGAL

When to inherit? A question soon occurs, when do I want to use inheritance from a class and when do I want to just have an object of the class be an instance variable? If you have a relationship between two classes that is an “is a” relationship then inheritance would be good. If the relationship is a “has a” relationship, then inheritance probably wouldn’t be good.

When to inherit examples A Student is a Person Student inherits from Person. A Student has a Pencil. Student does not inherit from Pencil. They have an instance variable of type Pencil. A Pencil is a WritingInstrument Pencil inherits from WritingInstrument. A Pencil is Yellow. Yellow is a single property of Pencil. Pencil doesn’t inherit from Yellow.

Review- How to inherit Which of the following can be inherited from this class? public class muglets { private int cost; public String name; public getCost() {…} private modifyCost(int something) }

Review- How to inherit Which of the following are legal if color is an inherited private variable, size(int) is an inherited public variable and all the methods (getSize, setSize, etc.) were public methods in Shape? … Rectangle r = new Rectangle(); r.size = 5; r.getSize(); r.setSize(15); r.color = “Red”; r.setColor(“Red”); r.getColor(); ...

Review- How to inherit Which of the following should inherit from one another(which way should they inherit?)? Which should just be instance variables? Horse. Animal. Computer. Keyboard. GamePiece. ChessPiece. LightSource. Lamp.

Overriding methods

Overriding Sometimes when we inherit methods, we wish to change the method slightly so that it is more meaningful for our class. We can do this with overriding. Overriding is when an derived(child) class writes out an alternate definition for a method from the base class. The two have the same name, and the same parameters. Note this is not overloading, as the two methods do not differ in their parameters.

Overriding Imagine there was a method in Shape: Then we could override that in rectangle with the following method: public void writeOutput() { System.out.println(“I am a shape.”); } public void writeOutput() { System.out.println(“I am a rectangle.”); }

Overriding example. … Shape a = new Shape(); Rectangle b = new Rectangle(); a.writeOutput(); //I am a shape. b.writeOutput(); //I am a rectangle.

Overriding We can override any kind of public method from our ancestor classes. Even constructors. We will talk more about overriding constructors later.

Overriding Java will figure out at run-time which of the methods you wanted to call(the base class one or the overridden one) when you invoke a method in some code. It will determine this by checking to see what class was used with “new” to originally initialize the object. We have actually been doing overriding already, whenever we create an equals method in our classes. We are overriding the equals methods of the Object class.

Object class. The Object class is an ancestor to EVERY class in Java. There are methods that we have already been inheriting that are useful, such as toString and equals. toString tells Java how to output the object to a string when it is concatenated with a string.

Review-Overriding Write a method for the Square class that will override the writeOutput method. Have it output “I am a square.”

Other details about inheritance.

How to stop inheritance If you include the modifier “final” in a method declaration or a class declaration, you will keep anyone from overriding that function or inheriting from that class, respectively. public final class Square {…}

Constructors in derived classes. A constructor in a derived class will automatically call the default constructor of its base class as the first step in the constructor. The only way out of this is to use “this” or “super” (a couple more slides).

Constructors in derived classes public Shape() { //default constructor for Object automatically //called. System.out.println(“I’m making a Shape.”); } … and then in another file. public Rectangle() { //default constructor for Shape automatically //called. System.out.println(“I’m making a rectangle.”); } Rectangle r = new Rectangle(); //I’m making a Shape. //I’m making a rectangle.

Calling an base class constructor- super. We can use “super” to refer to the base class. We can directly call a constructor by saying super(parameters); public Rectangle() { super(); ... } public Rectangle() { ... } =

Calling other constructors in the same class- this. We can also call a different constructor inside of the current class by using the “this” keyword. Just say “this(parameters)” and supply the appropriate parameters. See the Die example.

Rules about this and super in constructors. They must be used in the very first line of the constructor. They are not valid in any other position. public Rectangle() { super(); //Legal this(4,5); //ILLEGAL }

Calling an overridden method -super again. We can also call methods that we have overridden with a new method in our derived class. To refer to the base class’s method we say “super.methodName(parameters)” Doesn’t need to be the first line of the method, that is just a rule for constructors. public void writeOldOutput() //in Rectangle. { super.writeOutput(); //I am a shape. }

Objects of derived classes have more than one type. Objects of derived classes have all the types of all their ancestors. Thus all objects are of type Object. A Square is also of type Rectangle, Polygon, and Shape. You can cast or store an object to any of its types.

Inheritance Review

Inheritance Review Shape Polygon Squiggly Quadri- lateral Triangle Rectangle Square

Inheritance Review Which of the following should inherit from one another(which way should they inherit?)? Which should just be instance variables? Horse. Animal. Book. Chapter. Sports Equipment. Skis.

Inheritance Review Which of the following can be inherited from this class? public class muglets { private int cost; public String name; public getCost() {…} private modifyCost(int something) }

Inheritance Review Which of the following are legal if color is an inherited private variable, size(int) is an inherited public variable and all the methods (getSize, setSize, etc.) were public methods in Shape? … Rectangle r = new Rectangle(); r.size = 5; r.getSize(); r.setSize(15); r.color = “Red”; r.setColor(“Red”); r.getColor(); ...

Inheritance Review What methods can we override from a class? How do we override a method called “blar” that takes one integer parameter and returns a double? What word do we use to stop any class from inheriting from one of our classes? What word do we use to stop any child classes from overriding one of our methods?

Inheritance Review What is the super(w,h) instruction doing? public class Rectangle extends Quadrilateral { public Rectangle(int w, int h) super(w, h); } What is the super(w,h) instruction doing? How would we call the overridden method writeOutput (takes not parameters) of our base class?

Inheritance Review Which of the following types is the variable a? Rectangle a = new Rectangle(); Which of the following types is the variable a? Shape Quadrilateral Rectangle Square