Java Inheritance.

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

Classes and SubClasses Super Parent Sub Child IS - A.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
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 04 / 16 / 2010 Instructor: Michael Eckmann.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Computer Science I Inheritance Professor Evan Korth New York University.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Intro to OOP with Java, C. Thomas Wu
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Inheritance.
Advanced Programming in Java
Object-Oriented Programming: Inheritance
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance and Polymorphism
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
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.
Lecture 14 - Abstract Classes
Chapter 9 Inheritance and Polymorphism
Java Inheritance.
Extending Classes.
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Fundaments of Game Design
Lecture 6 Inheritance CSE /26/2018.
Chapter 11 Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Chapter 8 Inheritance Part 2.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
Lecture 6 Inheritance CSE /26/2018.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Java Inheritance

Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses Be able to use the keyword extends to create a class that inherits attributes and behaviors from another class. Understand how constructors are used in inheritance hierarchies. Understand the class Object, the direct or indirect superclass of all classes in Java.

Inheritance Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality.

“Is a…” vs. “has a…” “is a…” represents inheritance. A car “is a” vehicle. A Yugo “is a” car “Has a…” represents data. A car “has a” steering wheel. A truck “has a” tow rating.

Inheritance Hierarchy Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass Vehicle Steering wheel Engine Size Color Superclass Base class Generic class Can access public and protected from the superclass Car Type (Sedan, convertible wagon) Truck Tow rating Ford F150 Toyota T100 Dodge Dakota 5 Series Yugo Accord

‘Object’ class (Super duper) All classes in Java inherit directly or indirectly from the Object class. So every class has the following methods .equals: Compares two objects for equality and returns true if they are equal and false otherwise. object1.equals(object2)(If this method is not redefined in the subclass, then it will compare addresses of the objects, just like ==) .toString(): Returns a string representation of an object.

Other ‘Object’ methods clone finalize getClass hashCode notify, notifyAll, wait These are not in the AP level A exam subset.

Organizing into Inheritance Hierarchy Mountain Bike, Bicycle, Road bike, Tire Camel, height, Animal, weight, Cow, Hereford

What do a snake and a cow have in common? They are both animals They both have names They both make sounds But… A snake can have a rattle… For the discussion we will create an ‘Animal’ class out of what is common between a cow and a snake. Then use these in creating classes for the cow and snake. Using inheritance!

Inheritance Hierarchy Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass Superclass Base class Generic class Animal Name Sound Snake Rattle? Cow

Some Super Rules A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass using a super() call. (Unless no constructor is defined, then the parent’s default constructor is called)

What children (subclasses) can do. The inherited public and protected fields (variables) can be used directly, just like any other fields. You can declare a field (variable) in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The public and protected inherited methods can be used directly as they are. You can write a new instance (non static) method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. You can access public and protected fields and methods of the super class.

What children can’t do Access private fields or methods of a super class. If parents lock the safe, children can’t get into it without permission (a public accessor method) Limit the access (make a public method into a private method) when overriding a method. If parents say anyone can have cookies, then children cannot lock up the cookie jar.

Inheritance Hierarchy Subclass Inherits all of the members Extends the Superclass (Data and methods) from it’s superclass Superclass Base class Generic class Animal Name Sound Snake Rattle? Cow

Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses Be able to use the keyword extends to create a class that inherits attributes and behaviors from another class. Understand how constructors are used in inheritance hierarchies. Understand the class Object, the direct or indirect superclass of all classes in Java.

What do you recall about…? Class Parent Child Object Constructor Inheritance Public Protected “Bring out your notes…”

Now the ‘SUPERCLASS!” Can you change myName from a subclass of Animal? public class Animal { private String myName; //Subclasses must use getName to see this private String mySound; public Animal () //Default Constructor myName = ""; mySound = ""; } public Animal (String newName, String newSound) //Constructor that takes a name and sound myName = newName; mySound = newSound; public String getName() //Accessor Methods return myName; public String getSound() return mySound; public String toString() //toString returns the animals information return myName + " says " + mySound; Now the ‘SUPERCLASS!” Can you change myName from a subclass of Animal? Can you call getSound from a subclass of Animal? Can also write return this.getName() + “ says “ + this.getSound();

What methods do Cows have? Now the Cow public class Cow extends Animal { //All of the instance variables are inherited from the Animal class //Constructor public Cow (String newName, String newSound) //It will use Animal’s constructor to create the cow super(newName, newSound); //Calls the superclass's constructor } What methods do Cows have? Constructing Cows The constructor will call the superclasses constructor either with the super method (must be the first line of the constructor) or it will call the superclasses default constructor (implicitly, that is you don’t write the super call). If you do not define a constructor in the subclass, it will call the superclass’s default constructor when the subclass object is created.

Snake public class Snake extends Animal { private boolean myRattle; // //Constructor public Snake (String newName, String newSound, boolean rattle) super(newName, newSound); myRattle = rattle; } //Will need to overwrite toString so it will display stripe information public String toString() String message =("The snake " + getName() + " says "); message += getSound() + " and rattle = " + myRattle; return message; Make the call to the super first in the constructor. Why can’t you just write myName for the name? You can also use, this.getSound() and this.getName().

Driving this zoo public class AnimalDriver { public static void main(String args[]) Cow betsy = new Cow("Betsy", "moo"); System.out.println(betsy); Snake terry = new Snake("Terry", "hiss", TRUE); System.out.println(terry); }

Review What can subclasses do? What is ‘this’? Can a child redefine a parent’s methods? Can a subclass access all of the superclass methods What methods are in all classes? How do you call the parent class constructor from one of its children? How many parents can a child have? How many children can a parent have? What is a class variable? What is an instance variable?

Your Assignment: Animals Add two more classes of animals Enhance each class with at least one more instance variable. At least one of these classes must extend another class you created. Create a driver class (application, public static void main…) that will demonstrate that your classes are working. Turn in each of your classes and the driver class.