Inheritance in the Java programming language J. W. Rider.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using 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.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Programming
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
Inheritance and Polymorphism
CIS 199 Final Review.
Chapter 11 Class Inheritance
Presentation transcript:

Inheritance in the Java programming language J. W. Rider

Object-oriented features Encapsulation Inheritance Polymorphism

Inheritance Inheritance means that we never have to start over from scratch when defining a new class that similar to an existing class.

Class/Interface name usage Import statement Extends clause Implements clause Constructor header/reference Static member reference Method return type Variable type Typecasting Class testing with instanceof Defines “ancestry” of class Tests “ancestry” of object

Ancestry Extends –One class may extend another class –One interface may extend another interface Implements –A class may implement zero, one or many interfaces.

Super/sub classes Ancestry establishes a super-class/subclass relationship between two classes/interfaces. Super class Subclass “extends” “parent” “child”

Extends vs Implements Super class Subclass Implementation1 Implementation2 ImplementationN “extends” “implements” …

Object class is origin of all extensions Object C1 C3 Extends C2 I1 I2 C2 I3 Extends I2

Single Inheritance / Multiple Ancestry Object MyClass MySuperParent MyGrandParent

Encapsulation revisited A “class” is an encapsulation of its members. An “object” is an encapsulation of ALL of the non-static fields defined in its instantiated class and its ancestry.

Class qualifiers Public Abstract Final

The abstract qualifier Abstract methods contain no body. –The method header terminates with a semicolon rather than an open brace. A method with an empty body (vice “no body”) would have the header terminated with {}. Abstract classes may not be instantiated. –Frequently, but NOT required, abstract classes contain abstract methods. Concrete methods may be implemented. Constructors may be included. Abstract methods are not required. –A class that contains an abstract method MUST be declared abstract itself. –In order to create an object based upon the abstract class, the class needs to be extended by another class.

The final qualifier A final class may never be extended. A final method may never be overridden. A final variable may never be assigned a value.

The super keyword On the first executable line of a constructor, super is used as the name of the constructor of the super class. When dereferenced, super permits overridden methods in the super class to be called.

Overloading/overriding methods Overloading –Same method name, different formal parameters –Compiler determines which method gets called. Overriding –Same method name, same formal parameters –Only a subclass may override the method of an ancestor. –Object determines which method gets called.

The protected qualifier As a member qualifier, protected is accessible by a subclass, but not by other classes out of the package of the super class. Protected scope is often used for methods that are expected to be overridden by subclasses.

Polymorphism From the Greek for “accommodating many shapes”, Polymorphism supports programmers in writing code that accommodates objects from different classes.

Polymorphic features Subclass assignment compatibility –Instanceof operator “Virtual” methods –Dynamic or late binding

Subclass assignment compatibility A reference value may be assigned to a variable whose type is derived from an ancestor class or interface. MySuperClass x = new MySubClass(); The compiler uses only the type of the variable to determine what messages may be sent to the object.

The instanceof operator Object-reference instanceof Classname Returns a Boolean result of true if the referenced object has Classname as one of its ancestors.

“Virtual” methods The compiler does not choose which possibly-overridden method might be called when a method is not-static, not- private, and not-final. Instead, the object is sent a message, and the object decides what method should be invoked.

Design consideration Suppose we desire two constructible classes (A and B) to share a considerable amount of common code. If instances of A are assignable to variables of type B, have class A extend class B and make any changes within A. If instances of A and B are not assignable to variables of the other type, create a mutual abstract class and extend both A and B from the abstract class.

Summary Inheritance is just one of many ways to relate two classes with each other. Inheritance can keep us from writing duplicate code for classes. Inheritance allows us to write polymorphic code that uses a common algorithm for solving similar problems.