Inheritance.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Inheritance Reserved word protected Reserved word super
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
LECTURE 07 Programming using C# Inheritance
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance ndex.html ndex.htmland “Java.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
BY:- TOPS Technologies
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
03/10/14 Chapter 9 Inheritance.
Week 4 Object-Oriented Programming (1): Inheritance
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Inheritance Chapter 5.
More inheritance, Abstract Classes and Interfaces
CMSC 202 Interfaces.
Java Programming Language
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
INHERITANCE BASICS Reusability is achieved by INHERITANCE
CS18000: Problem Solving and Object-Oriented Programming
مظفر بگ محمدی دانشگاه ایلام
CMSC 202 Interfaces.
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Topics OOP Review Inheritance Review Abstract Classes
CMSC 202 Interfaces.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Inheritance and Polymorphism
Presentation transcript:

Inheritance

Inheritance Keywords Learn about inheritance in Java programming. extends this super

Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class

Inheritance In Object-Oriented Programming (OOP) this is-a relationship provides the basis for Inheritance Inheritance is the process in which one class acquires the property of another class. This is what Object Oriented Programming is all about! Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes.

Top Down Design

Top-Down Programming (cont’d) Start JCreator. Create a new file called “Animal.java”. Save the new file in your Lab 03/Jungle folder.

Inheritance – Base Class The Animal class will be the base class (or super class) for all the animals in the Jungle. public class Animal { } This is what Object Oriented Programming is all about!

Inheritance – Base Class The Animal class will be the base class (or super class) for all the animals in the Jungle. public class Animal { private String type; } This is what Object Oriented Programming is all about!

Inheritance – Base Class The Animal class will be the base class (or super class) for all the animals in the Jungle. public class Animal { private String type; public Animal(String type) { this.type = type; } This is what Object Oriented Programming is all about!

Inheritance – Base Class The Animal class will be the base class (or super class) for all the animals in the Jungle. public class Animal { private String type; public Animal(String type) { this.type = type; } public void eat() { System.out.println("I like to eat, don't I?"); This is what Object Oriented Programming is all about!

Inheritance – Base Class The Animal class will be the base class (or super class) for all the animals in the Jungle. public class Animal { private String type; public Animal(String type) { this.type = type; } public void eat() { System.out.println("I like to eat, don't I?"); public void speak() { System.out.println(“I am a ” + type + “.”); This is what Object Oriented Programming is all about!

Inheritance – Base Class All Animal objects can eat and speak because eat and speak are methods of the Animal class. public class Animal { public void eat() { /* code not shown */ } public void speak() { /* code not shown */ } } This is what Object Oriented Programming is all about! Animal a = new Animal(); a.eat(); a.speak();

Create a new file called “Jungle.java”. Save “Jungle.java” in your Lab 03/Jungle folder.

Deriving Subclasses public class Jungle { } private Animal[ ] animals = new Animal[10]; private int count = 0; public static void main (String[ ] args) { Jungle jungle = new Jungle(); jungle.output(); } public Jungle() { animals[count++] = new Animal(“Cat”); } public void output() { System.out.println(“--- In The Jungle, The Mighty Jungle ---\n”); for (int i = 0; i < count; i++) { animals[i].speak(); animals[i].eat(); System.out.println(); }

Deriving Subclasses Compile Animal Compile Jungle. Run Jungle. --- In The Jungle, The Mighty Jungle--- I am a Cat. I like to eat, don’t I?

Inheritance Software reuse is a fundamental benefit of inheritance By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software

Create a new file called “Cat.java”. Save the new file in your Lab 03/Jungle folder.

Deriving Subclasses public class Cat extends Animal { } In Java, we use the reserved word extends to establish an inheritance relationship. public class Cat extends Animal { } A Cat “is a” Animal.

But Animals are not necessarily Cats! Deriving Subclasses Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent Animal Cat A Cat is an Animal. But Animals are not necessarily Cats!

Deriving Subclasses All Cat objects can eat and speak because Cat extends Animal and eat and speak are methods of the Animal class. Cat has inherited eat and speak. public class Animal { public void eat() { /* code not shown */ } public void speak() { /* code not shown */ } } This is what Object Oriented Programming is all about! Cat c = new Cat(); c.eat(); c.speak();

Deriving Subclasses public class Cat extends Animal { Sub class can define new instance fields. public class Cat extends Animal { public String name; } This is what Object Oriented Programming is all about!

The super Reference Constructors are not inherited, even though they have public visibility Yet we often want to use the parent's constructor to set up the "parent's part" of the object The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor

The super Reference A child’s constructor is responsible for calling the parent’s constructor The first line of a child’s constructor should use the super reference to call the parent’s constructor The super reference can also be used to reference other variables and methods defined in the parent’s class

The super Reference public class Cat extends Animal { In Java, we use the reserved word super to refer to the parent class. public class Cat extends Animal { public String name; public Cat(String name) { super(“Cat”); } super is a call to the super constructor

The this Reference public class Cat extends Animal { The keyword this refers to the object. Thus this.name is the instance field while name is the local variable. public class Cat extends Animal { public String name; public Cat(String name) { super(“Cat”); this.name = name; } This is what Object Oriented Programming is all about!

Overriding Methods A child class can override the definition of an inherited method. The new method must have the same signature as the parent's method, but can have a different body. A method in the parent class can be invoked explicitly using the super reference.

Overriding Methods Methods in the sub class can override methods in the super class. public class Cat extends Animal { public String name; public Cat(String name) { super("Cat"); this.name = name; } @Override public void speak() { System.out.println(“My name is ”+ name + “.”); super.speak(); This is what Object Oriented Programming is all about!

Top-Down Programming (cont’d) Open the file called “Jungle.java”.

Deriving Subclasses In the Jungle class, the array animals is an array of Animal objects. private Animal[ ] animals = new Animal[5]; Since any class that inherits from Animal “is a” Animal, animals can contain instances of any class that extends Animal.

Deriving Subclasses public Jungle() { animals[count++] = new Animal(“Cat”); animals[count++] = new Cat(“Fluffy”); } public void output() { System.out.println(“--- In The Jungle, The Mighty Jungle ---\n”); for (int i = 0; i < count; i++) { animals[i].speak(); animals[i].eat(); System.out.println();

Deriving Subclasses Compile Cat Compile Jungle. Run Jungle. --- In The Jungle, The Mighty Jungle--- I am a Cat. I like to eat, don’t I? My name is Fluffy.

Create a new file called “Lion.java”. Save the new file in your Lab 03/Jungle folder.

Deriving Subclasses public class Lion extends Cat { }

Deriving Subclasses public class Lion extends Cat { public Lion(String name) { super(name); }

Deriving Subclasses public class Lion extends Cat { public Lion(String name) { super(name); } @Override public void speak() { super.speak(); System.out.println(“Hear me roar!”);

Top-Down Programming (cont’d) Open the file called “Jungle.java”.

Deriving Subclasses public Jungle() { animals[count++] = new Animal(“Cat”); animals[count++] = new Cat(“Fluffy”); animal[count++] = new Lion(“Leo”); } public void output() { System.out.println(“--- In The Jungle, The Mighty Jungle ---\n”); for (int i = 0; i < count; i++) { animals[i].speak(); animals[i].eat(); System.out.println();

Deriving Subclasses Compile Lion. Compile Jungle. Run Jungle. --- In The Jungle, The Mighty Jungle--- I am a Cat. I like to eat, don’t I? My name is Fluffy. My name is Leo. Hear me roar!

Top-Down Programming (cont’d) Open the file called “Lion.java”.

Deriving Subclasses The sub class can contain additional methods. public class Lion extends Cat { public Lion(String name) { super(name); } @Override public void speak() { super.speak(); System.out.println(“Hear me roar!”); public void goHunting() { System.out.println(“Quiet in the peanut gallery!”); System.out.println(“I’m trying to hunt here.”);

Top-Down Programming (cont’d) Open the file called “Jungle.java”.

Deriving Subclasses public Jungle() { animals[count++] = new Animal(“Cat”); animals[count++] = new Cat(“Fluffy”); animal[count++] = new Lion(“Leo”); } public void output() { System.out.println(“--- In The Jungle, The Mighty Jungle ---\n”); for (int i = 0; i < count; i++) { animals[i].speak(); animals[i].eat(); animals[i].goHunting(); System.out.println();

Deriving Subclasses Compile Lion. Compile Jungle. Run Jungle. C:\Users\jspic\Desktop\Jungle\Jungle\Jungle.java:25: error: cannot find symbol animals[i].goHunting(); ^ symbol: method goHunting()tion: class Animalr The Lion class contains a goHunting() method. The Animal class does NOT! While A Lion “is a” Animal, an Animal is NOT necessarily a Lion.

instanceof Operator The instanceof operator can be used to test if an object is of a specified type.

instanceof Operator An object is an instanceof the class from which is is created and any class that it extends or any interface that it implements.

Deriving Subclasses public Jungle() { animals[count++] = new Animal(“Cat”); animals[count++] = new Cat(“Fluffy”); animal[count++] = new Lion(“Leo”); } public void output() { System.out.println(“--- In The Jungle, The Mighty Jungle ---\n”); for (int i = 0; i < count; i++) { animals[i].speak(); animals[i].eat(); if (animals[i] instanceof Lion) Lion cat = (Lion) animals[i]; cat.goHunting(); System.out.println();

Deriving Subclasses Compile Leopard Compile Jungle. Run Jungle. --- In The Jungle, The Mighty Jungle --- I am a Cat. I like to eat, don't I? My name is Fluffy. My name is Leo. Hear me roar! Quiet in the peanut gallery! I'm trying to hunt here.

Create a new file called “Leopard.java”. Save the new file in your Lab 03/Jungle folder.

Deriving Subclasses A super class can be extended by multiple sub classes. public class Leopard extends Cat { }

Deriving Subclasses A super class can be extended by multiple sub classes. public class Leopard extends Cat { public Leopard(String name) { super(name); }

Deriving Subclasses A super class can be extended by multiple sub classes. public class Leopard extends Cat { public Leopard(String name) { super(name); } @Override public void speak() { super.speak(); System.out.println(“I'm a sneaky little devil!”);

Deriving Subclasses A super class can be extended by multiple sub classes. public class Leopard extends Cat { public Leopard(String name) { super(name); } @Override public void speak() { super.speak(); System.out.println(“I'm a sneaky little devil!”); public void goHunting() { System.out.println(“As quiet as a mouse!”); System.out.println(“I lurk in the trees.”);

Top-Down Programming (cont’d) Open the file “Jungle.java”.

Deriving Subclasses public Jungle() { animals[count++] = new Animal(“Cat”); animals[count++] = new Cat(“Fluffy”); animals[count++] = new Lion(“Leo”); animals[count++] = new Leopard(“Panthera”); }

Deriving Subclasses Compile Leopard Compile Jungle. Run Jungle. --- In The Jungle, The Mighty Jungle --- I am a Cat. I like to eat, don't I? My name is Fluffy. My name is Leo. Hear me roar! Quiet in the peanut gallery! I'm trying to hunt here. My name is Panthera. I'm a sneaky little devil!

Deriving Subclasses public void output() { System.out.println(“--- In The Jungle, The Mighty Jungle ---\n”); for (int i = 0; i < count; i++) { animals[i].speak(); animals[i].eat(); if (animals[i] instanceof Lion) Lion cat = (Lion) animals[i]; cat.goHunting(); } else if (animals[i] instanceof Leopard) Leopard cat = (Leopard) animals[i]; System.out.println();

Deriving Subclasses Compile Jungle. Run Jungle. --- In The Jungle, The Mighty Jungle --- I am a Cat. I like to eat, don't I? My name is Fluffy. My name is Leo. Hear me roar! Quiet in the peanut gallery! I'm trying to hunt here. My name is Panthera. I'm a sneaky little devil!

Multiple Inheritance Java does not support multiple inheritance

Questions?

Begin Lab 03