Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

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.
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 Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10: Introduction to Inheritance
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Object Oriented Programming: Inheritance Chapter 9.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSE 143 Lecture 6 Inheritance; binary search reading: 9.1, ; 13.1 slides created by Marty Stepp
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
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 and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
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.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
BY:- TOPS Technologies
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Understanding Inheritance
Inheritance Chapter 5.
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Java – Inheritance.
Java Programming, Second Edition
Inheritance.
Advanced Inheritance Concepts
Inheritance and Polymorphism
Presentation transcript:

Inheritance

In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that have constructors Using superclass constructors that require arguments Accessing superclass methods Learning about information hiding Using methods you cannot override

Learning about the Concept of Inheritance Inheritance –Mechanism that enables one class to inherit both the behavior and the attributes of another class The classes you create can inherit data and methods from existing classes You can apply your knowledge of a general category to a more specific category What is an example of when you would use inheritance?

Inheritance Advantages When you use inheritance you: –Save time –Reduce errors –Ease understanding

Inheritance Terms Base class – a class that is used as a basis for inheritance –Superclass –Parent class –e.g. Employee class Derived class – a class that inherits from a base class –Subclass –Child class –e.g. Manager class Manager has an “is-a” relationship to Employee. Subclasses have more functionality than superclasses. –Subclasses can use the fields and methods of superclass and can add additional fields and methods.

Extending classes Use the keyword extends to achieve inheritance within the Java programming language –public class EmployeeWithTerritory extends Employee Creates a superclass-subclass relationship between Employee and EmployeeWithTerritory You only need to indicate the differences between the subclass and the superclass in the subclass definition.

What is Polymorphism?

Overriding Superclass Methods Polymorphism –Using the same method name to indicate different implementations Means “many forms” Each child class method overrides the method that has the same name in the parent class –e.g. getSalary method is different for managers than from other employees. should return the base salary plus the bonus because the subclass Manager cannot access private fields of superclass, it needs to use the public method getSalary(). to differentiate from the subclass definition of this method we have to use super.getSalary().

Working with Superclasses that Have Constructors When you create any subclass object, the superclass constructor must execute first, and then the subclass constructor executes When you instantiate an object that is a member of a subclass, you are actually calling at least two constructors: – the constructor for the base class – the constructor for the extended, derived class

Using Superclass Constructors that Require Arguments When you create a class and do not provide a constructor, Java automatically supplies you with one that never requires arguments When you write your own constructor, you replace the automatically supplied version The constructor you create for a class might require arguments

Using Superclass Constructors that Require Arguments When a superclass requires arguments, you must include a constructor for each subclass Your subclass constructor can contain any number of statements, but the first statement must call the superclass constructor –super(list of arguments); –For example, because subclasses cannot access private fields of superclass, it must initialize them thru the superclass constructor: super(n, s, year, month, day) Keyword super always refers to the superclass of the class in which you use it

Accessing Superclass Methods You might want to use the superclass method within a subclass You can use the keyword super to access the parent class method for example: public class Manager extends Employee { public Manager(String name, int salary) { super(name, salary); bonus=0; } }

Information Hiding –The concept of keeping data private When you employ information hiding, your data can be altered only by the methods you choose and only in ways that you control Private members of the parent class are not inherited –When a program is a class user, it cannot directly alter any private field

Methods You Cannot Override There are four types of methods that you cannot override in a subclass: –private methods –static methods –final methods –Methods within final classes –Final methods and classes have some advantages: Efficiency: Dynamic binding has more overhead and runs slower. Safety: Designer has no control over what a method will do in a subclass. Final methods guarantee what the method wil do when called.