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

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.
Inheritance Inheritance Reserved word protected Reserved word super
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
 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.
What is inheritance? It is the ability to create a new class from an existing class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Java Inheritance.
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance and Polymorphism
ATS Application Programming: Java Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Java Inheritance.
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
Fundaments of Game Design
Lecture 6 Inheritance CSE /26/2018.
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
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
A couple of practice problems as a class
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.

Java uses the key word extends for inheriting from a class. public class Location { public Location(int xCoord, int yCoord) . . . } public class Color public Color(int redVal, int greenVal, int blueVal) public class Widget private Location myLoc; public Widget(int x, int y) myLoc = new Location(x, y); public class Thingy extends Widget private Color myColor; public Thingy(int x, int y, Color col) super(x, y); myColor = col; Example Java uses the key word extends for inheriting from a class. Color, Location, Widget, Thingy

“Is a…” vs. “has a…” “is a…” represents inheritance. A car “is a” vehicle. A Yugo “is a” car “Has a…” represents data/ instance variables/fields. 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 Dodge 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-variables, 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 (name and parameters) 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.

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

Time to Start a Farm Write a class that defines an Animal. It will need at least the following Constructors Default and Given name and sound. State - variables Fields for name and sound Behavior – non-static methods getName(), getSound(), toString()

Inheritance Hierarchy Now for the Cow Animal Name Sound Snake Rattle? Cow

Inheritance Hierarchy Now for the Snake Animal Name Sound Snake Rattle? Cow

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?

Tracing Code in Classes Constructors If there is a super() call, it must be the first line of the constructor. If there is no explicit super() call, it calls the parent’s default constructor If there is a super() call, it calls the corresponding constructor in the parent.

public class Location { public Location(int xCoord, int yCoord) public class Location { public Location(int xCoord, int yCoord) . . . } public class Color public Color(int redVal, int greenVal, int blueVal) public class Widget private Location myLoc; public Widget(int x, int y) myLoc = new Location(x, y); public class Thingy extends Widget private Color myColor; public Thingy(int x, int y, Color col) super(x, y); myColor = col; Assume that the following statement appears in a client program. Widget widg = new Thingy(100, 100, new Color(100, 100, 100)); What best describes the order on which the constructors will complete execution? Color, Location, Widget, Thingy

Your Assignment: More 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. Push: Create a group of animals in the application. Array, Arraylist, A Barn class that stores several animals, …