Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance.

Similar presentations


Presentation on theme: "Inheritance."— Presentation transcript:

1 Inheritance

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

3 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

4 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.

5 Top Down Design

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

7 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!

8 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!

9 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!

10 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!

11 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!

12 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();

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

14 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(); }

15 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?

16 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

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

18 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.

19 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!

20 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();

21 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!

22 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

23 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

24 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

25 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!

26 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.

27 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!

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

29 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.

30 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();

31 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.

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

33 Deriving Subclasses public class Lion extends Cat { }

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

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

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

37 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();

38 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!

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

40 Deriving Subclasses The sub class can contain additional methods.
public class Lion extends Cat { public Lion(String name) { super(name); 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.”);

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

42 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();

43 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.

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

45 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.

46 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();

47 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.

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

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

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

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

52 Deriving Subclasses A super class can be extended by multiple sub classes. public class Leopard extends Cat { public Leopard(String name) { super(name); 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.”);

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

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

55 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!

56 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();

57 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!

58 Multiple Inheritance Java does not support multiple inheritance

59 Questions?

60 Begin Lab 03


Download ppt "Inheritance."

Similar presentations


Ads by Google