Java Programming, Second Edition

Slides:



Advertisements
Similar presentations
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.
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 10: Introduction to Inheritance
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
 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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
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
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
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.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance and Access Control CS 162 (Summer 2009)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
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.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
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.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Introduction to Object-oriented Programming
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Road Map Inheritance Class hierarchy Overriding methods Constructors
Nested class.
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
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Programming: Inheritance
Advanced Java Programming
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Object-Oriented Programming: Inheritance and Polymorphism
Class Inheritance Concept of Inheritance Java keywords
Inheritance.
Advanced Inheritance Concepts
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Java Programming, Second Edition Chapter Eleven Introduction to Inheritance

In this chapter, you will: Learn about the concept of inheritance Extend classes Override superclass methods Work with superclasses that have constructors Use superclass constructors that require arguments Access superclass methods Learn about information hiding Use 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

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 Derived class- a class that inherits from a base class Subclass Child class

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

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

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); 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

Information Hiding 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

Protected Protected- Provides you with an intermediate level of security between public and private access If you create a protected data field or method, it can be used within its own class or in any classes extended from that class, but it cannot be used by “outside” classes

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