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.

Slides:



Advertisements
Similar presentations
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Object-Oriented PHP (1)
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.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
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.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Coming up: Inheritance
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 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.
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 Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
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)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Inheritance and Polymorphism
One class is an extension of another.
One class is an extension of another.
Java Programming, Second Edition
Java Inheritance.
Inheritance.
Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

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 the functionality of an old class, you do not need to reinvent the wheel. All what you need is to inherit from that class and add the new features you want to introduce. The original class is called super, base or parent class while the new one is called derived or child class. When you create an object of the derived class it always contain a complete object of the parent one with all its data members and methods in addition to its own stuff.

3 Basics of Inheritance (Cont.) Inheritance is all about what you can access not what you have (Why??). To inherit from a base class you use the keyword “extends” when you define your derived class An object of the derived class do NOT have access (do not inherit) to the following: –Members or methods declared as private –Constructors of the parent class regardless their attributes –Members or methods declared as being friendly unless the child is defined in the same package In the constructor of the child class, you need to call the base class constructor using the keyword super. This call must be the first line If you do not do so, the compiler will insert a call for the default constructor Ex: Animal, Dog, TestDerived

4 Basics of Inheritance (Cont.) If you define a member or a method in the child class with the same exact name as the one in the parent THEN the derived class name will override (hide) the parent class one Overriding the base class method Ex: “TestDerived2” Inheritance also allows us to apply a principal called Polymorphism

5 Polymorphism The ability of a single variable of a given type to reference objects of other types Advantages: Among others, the ability to change program behavior at run time To polymorphically call a method, it must be defined in both the base and the derived class Also, the object type needs to be of the parent class but the stored object can be any of its children or grandchildren Ex: “TryPolymorphism” You can inherit from a child class and this can continue deeper and deeper to create an inheritance tree Ex: “TryPolymorphism2”

6 Abstract Classes A class can be declare as an abstract one using the key word “abstract” Abstract classes can NOT be instantiated One or more methods in an abstract class should be declared as abstract An abstract method is just a method definition without any implementation. This type of methods is used in order to allow children classes to override that method and make use of polymorphic calls Exercise: Convert the class Animal to an abstract one

7 Universal Superclass The Object class is the root for all classes in Java. (check its methods) We can determine the type of an object using the getClass() method of Object which returns an object of type Class and the getName() method of Class. To execute a method unique to a particular class you need to explicitly cast down the inheritance hierarchy. Ex: “LayEggs”

8 Casting & interfaces To identify the type of an object stored in a parent class member, you can use the instanceof operator In case you create an inheritance hierarchy just to take advantage of polymorphic calls, you might define the parent class as an Interface To take the concept of abstract classes one step farther, Java provides a way to make every method abstract by creating an interface instead of a class. An Interfaces does not define what a method does rather it just defines its form

9 Interfaces (Cont.) An interface can contain either constants or abstract methods or both To inherit from the interface, we use the keyword “implements” instead of “extends” Constants in an interface are referenced exactly as we do with static variables of a class Implementing an interface requires implementing all the methods defined into it Ex: “ TryConversions2”