Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.

Slides:



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

Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
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.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
© 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.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA BASICS Prepared by The Smartpath Information Systems
Inheritance in the Java programming language J. W. Rider.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 12 Advanced Inheritance
 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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
Classes, Interfaces and Packages
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Polymorphism in Methods
Sections Inheritance and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Java Programming Language
Polymorphism and access control
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Java Programming, Second Edition
Applying OO Concepts Using Java
Inheritance.
Advanced Inheritance Concepts
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts

In this chapter, you will: Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use the Object class and its methods Use inheritance to achieve good software design Create and use interfaces Create and use packages

Creating and Using Abstract Classes Abstract class- A class from which you cannot create any concrete objects, but from which you can inherit You can only extend abstract classes Use the keyword abstract You cannot use the keyword new

Creating and Using Abstract Classes Nonabstract classes from which objects can be instantiated are called concrete classes In other programming languages, such as C++, abstract classes are known as virtual classes

Abstract Methods Abstract method- A method with no method statements To create an abstract method, you provide the keyword abstract the intended method type, name, and arguments but you do not provide any statements within the method You must code a subclass method to override any inherited abstract superclass method

Using Dynamic Method Binding When you create a superclass and one or more subclasses, each object of the subclass “is a” superclass object Because every subclass “is a” superclass member, you can convert subclass objects to superclass objects

Using Dynamic Method Binding You can create a reference to a superclass But you do not use the keyword new You create a variable name to hold the memory address of a subclass concrete object

Using Dynamic Method Binding Dynamic method binding- The program’s ability to select the correct subclass method Is also called late binding

Creating Arrays of Subclass Objects You might want to create a superclass reference and treat subclass objects as superclass objects so you can create an array of different objects that share the same ancestry Manipulate an array of subclass objects by invoking the appropriate method for each subclass Elements in a single array must be of the same type

Using the Object Class and Its Methods Every class in Java is a subclass except for the Object class The Object class is defined in the java.lang package java.lang is automatically imported every time you write a program The Object class includes methods that you can override

Using the Object Class and Its Methods toString Method- If you do not create a toString() method for a class, then you can use the superclass version of the toString() method Can be useful for debugging equals() method- Takes a single argument, which must be the same type as the type of the invoking method Returns a Boolean value

Using Inheritance to Achieve Good Software Design Extended superclass advantages Subclass creators save development time Subclass creators save testing time Programmers who create or use new subclasses already understand how the superclass woks, so the time it takes to learn the new class features is reduced When you create a new subclass in Java, neither the superclass source code nor the superclass bytecode is changed; the superclass maintains its integrity

Creating and Using Interfaces Multiple inheritance- The capability to inherit from more than one class Multiple inheritance is prohibited in the Java programming language because it is problematic Java provides an alternative to multiple inheritance: an interface

Creating and Using Interfaces Interface- Looks much like a class, except all of its methods must be abstract and all of its data (if any) must be static final Use the keyword implements and the interface name in the class header implements exposes elements of the program to the user without exposing the source code

Creating and Using Packages Creating packages encourages others to reuse software because it makes it convenient to import many related classes at once When you create a number of classes that inherit from each other, you will often find it convenient to place these classes in a package

Creating and Using Packages Include a package statement at the beginning of the class file to place compiled code in the indicated folder The package statement must appear outside the class definition When compiling a file that you want to place in a package, use the –d compiler option with the javac command