Inheritance.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Class Inheritance Liang, Chpt 8. X is a Y: the is-a hierarchy life-form animal person clown geometric object circle cylinder instrument trumpet piano.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Intro to OOP with Java, C. Thomas Wu
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
EKT472: Object Oriented Programming Overloading and Overriding Method.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Class Inheritance Part I
Chapter 15 Abstract Classes and Interfaces
OOP: Encapsulation &Abstraction
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Final and Abstract Classes
Inheritance and Polymorphism
Advanced Programming in Java
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 9 Inheritance and Polymorphism
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9 Inheritance and Polymorphism
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 7 Inheritance.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Inheritance

Agenda Inheritance Creating a Subclass Overriding and overloading Final keyword

Classes and Inheritance

Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. Inheritance represents the is a relationship, an object of a subclass also can be treated as an object of its superclass.

Superclasses and Subclasses

Why inheritance To take advantage code reuse. To use polymorphism.

Example // Circle.java: Class definition for describing Circle public class Circle { private double radius; public Circle() {} public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; /** Set a new radius */ public void setRadius(double radius) { /** Return area */ public double findArea() { return radius * radius * Math.PI;

Example, cont. UML Diagram // Cylinder.java: Class definition for describing Cylinder public class Cylinder extends Circle { private double length = 1; /** Return length */ public double getLength() { return length; } /** Set length */ public void setLength(double length) { this.length = length; /** Return the volume of this cylinder */ public double findVolume() { return findArea() * length; UML Diagram

Example, cont. The output is: Cylinder cylinder = new Cylinder(); System.out.println("The length is " + cylinder.getLength()); System.out.println("The radius is " + cylinder.getRadius()); System.out.println("The volume of the cylinder is " + cylinder.findVolume()); System.out.println("The area of the circle is " + cylinder.findArea()); The output is: The length is 1.0 The radius is 1.0 The volume of the cylinder is 3.14159 The area of the circle is 3.14159

Inheritance Hierarchy A class may have several subclasses and each subclass may have subclasses of its own. The collection of all subclasses descended from a common ancestor is called an inheritance hierarchy.

Creating a Subclass

Creating a Subclass Creating a subclass extends properties and methods from the superclass. You can also: Add new properties Add new methods Override the methods of the superclass.

Using the Keyword super The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor To call a superclass method

Are superclass’s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly from subclass. Explicitly using the super keyword. Implicity if super is not used.

CAUTION You must use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor.

Overriding and overloading

Overriding Methods A subclass inherits methods from a superclass. Sometimes it is necessary to modify the method defined in the superclass. This is referred to as method overriding.

Overriding Methods public class Circle { private double radius; public Circle() {} public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; /** Set a new radius */ public void setRadius(double radius) { /** Return area */ public double findArea() { return radius * radius * Math.PI;

Overriding Methods // Cylinder.java: New cylinder class that overrides the findArea() // method defined in the circle class. public class Cylinder extends Circle {  /** Return the surface area of this cylinder. The formula is * 2 * circle area + cylinder body area */ public double findArea() { return 2 * super.findArea() + 2 * getRadius() * Math.PI * length; } // Other methods are omitted

Overriding a method rules The argument list must exactly match.(If they don't match, call it overloaded). The return type must be the same. The access level can't be more restrictive. The access level can be less restrictive.

Overriding a method rules, cont. Instance methods can be overridden only if they are inherited. A subclass within the same package can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked public or protected. You cannot override a method marked final.

Method Overloading Method overloading: having multiple methods with the same name but different signatures in a class. You can overload superclass method public class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } public class Horse extends Animal { System.out.println("Horse eating hay "); public void eat(String s) { System.out.println("Horse eating " + s);

Overriding vs. Overloading

Visibility modifiers

Visibility Modifiers

Visibility Modifiers

Final keyword

The final Modifier The final class cannot be extended: final class Math { ... } The final variable is a constant: final static double PI = 3.14159; The final method cannot be overridden by its subclasses.

Questions

Thanks