Inheritance Writing and using Classes effectively.

Slides:



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

Introduction to Java 2 Programming
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Object Oriented Programming
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Using Classes to Store Data Computer Science 2 Gerb.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance Chapter 8.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Inheritance Chapter 9.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance, Abstract & Interface Pepper With help from and
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
CSC 142 Computer Science II Zhen Jiang West Chester University
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSC 142 Computer Science II Zhen Jiang West Chester University
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Polymorphism 11-Nov-18.
CS2102: Lecture on Abstract Classes and Inheritance
CSC 113 Tutorial QUIZ I.
Inherited Classes in Java
Inheritance.
CS18000: Problem Solving and Object-Oriented Programming
An Example of Inheritance
Presentation transcript:

Inheritance Writing and using Classes effectively

OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Write a subclass that extends a superclass Decide what belongs in superclass vs. subclass Write a method that overrides a superclass method Demonstrate the priority of methods of superclasses and subclasses

Vocabulary & Terms The words below will be covered in the following slides: inherit/inheritance extends superclass subclass this super override

Many people have pets, they’re nice. There are some things that all pets have in common. Give some thought to those things, and see some of them suggested on the next page.

What do all pets have? -Name (String) -Age (int) -Ability to speak The following slide has an example of the Pet class.

public class Pet { //variables private int age; private String name; //constructor public Pet() { age = 1; name = "Fido"; } //methods public void setName(String aName) { this.name = aName; } public String getName() { return name; } public void setAge(int ageNumber) { this.age = ageNumber; } public int getAge() { return this.age; } public String speak() { return "Hi there"; } } Compile this in DrJava, and then create a new object in the interactions panel and make it speak: Pet mine = new Pet(); System.out.println(mine.speak());

If you performed the last slide correctly, your Pet said “Hi there”. Pets don’t normally say “Hi there”. They usually speak unique sounds. Our next class “is a type of” Pet: a Bird. The following page is the Bird class.

public class Bird extends Pet { //constructor public Bird() { super(); } //methods public String speak() { return "Tweet Tweet"; } See the words “extends Pet” ? That means if Pet does something that you like, you can just use what Pet has, and you don’t have to rewrite it. Because Pet does it, you don’t have to write the setAge or getAge, setName or getName functions. Because you used the words “extends Pet”, you “inherit” everything Pet does!

public class Bird extends Pet { //constructor public Bird() { super(); } //methods public String speak() { return "Tweet Tweet"; } If you don’t like something Pet does, then you can rewrite it in Bird, like the speak() method. We didn’t want our Bird to say “Hi there” like the general Pet, we want it to say “Tweet Tweet”. Our Bird speak() method overrides the Pet speak() method. The other methods of Pet still work!

Pet is the superclass, Bird is the subclass. Compile Bird and Pet in DrJava. Create new Pet and Bird objects, then use the setAge(), speak(), and getAge() methods on each: Pet a = new Pet(); Bird b = new Bird(); a.setAge(3); b.setAge(5); System.out.println(a.getAge()); System.out.println(b.getAge()); System.out.println(a.speak()); System.out.println(b.speak()); Bird doesn’t have setAge() or getAge(), it uses the ones “inherited” from Pet!

Because your Bird is a “subclass”, and the Pet is the “superclass”, you can connect the Bird and Pet still. If you want the Bird class to have a method to use the Pet’s “speak” method, you need to use the word “super” to refer to it. The following slide has the method.

(In the Bird class) public String parentSpeak() { return super.speak(); } “super.speak()” is the reference from the Bird to the “speak()” method of the parent, Pet. Add this to your Bird class, and try using it!

public Bird() { super(); } You may have also noticed that super() is inside the constructor for Bird. This is a common habit if your constructor has nothing else. It helps to create the variables, etc., for your subclass.

What if your Bird class and Pet class and Dog class all use the same name for something? How do we avoid conflicts and confusion? For example, if your Bird has an age variable, and your Pet has an age variable, how is it ordered?

Your computer will assume that you want the variable or method of the object that calls it. For example, if your Bird has an age variable, and “bill” is a Bird, bill.getAge() should look for the age in Bird. But, you can use the “this” keyword to make sure.

Assume Pet has an “age” and Bird has an “age”. public int getAge() { return age; } in the Bird class assumes the age in Bird. To be explicit, you can say: public int getAge() { return this.age; }

Assume Pet has an “age” and Bird has an “age”. If you wanted to explicitly reference the age variable of the Pet (super) class, you would type it this way: public int getAge() { return super.getAge(); } (Use super for the superclass’ stuff, and this for the class’ stuff)

What order does it follow by default? If a subclass (like Bird) has a method (like setAge), then it would be called before the super’s method (like Pet’s setAge). If a subclass does not have a method, but the super does, then the super’s method is called automatically.

When writing your programs, think it through first, and you can save a lot of time. Think of all of the “things”, and if they have anything in common. If so, you can create a “superclass” that holds many of those things. If you then want to add a specialized subclass, you can add variables, methods, and even override methods!