Inheritance Java permits you to use your user defined classes to create programs using inheritance.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 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.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Topics Inheritance introduction
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Object-Oriented Programming: Inheritance and Polymorphism.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Java Programming, Second Edition
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

Inheritance Java permits you to use your user defined classes to create programs using inheritance.

Inheritance All classes in Java are organized into a class hierarchy. The highest level classes are very general and the lower level classes are more specific. The lower level classes are based upon the higher level classes and inherit the higher level class’ public and protected instance variables and methods. They also contain their own instance variables and methods beyond the higher level class definition.

Inheritance The higher level classes are called Superclasses and the new or lower level classes are called the subclasses. A subclass can always become a superclass Inheritance allows you to define certain behaviours once and then to reuse those behaviours over and over again in the subclasses. This is called reusability.

Inheritance Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and extending them with the capabilities that the new class requires.

Inheritance When a class definition inherits instance variables and methods from another class: –The other class with the original instance variables and methods is the super class. –The new class being defined is a subclass of the super class. –An example is to think of your parents as a super class and you as the subclass.

Inheritance CommunityMember Alumni Student Employee Faculty Staff Administrator Professor FreshmanSophomoreJuniorSenior

Inheritance public class CommunityMember {} public class Employee extends CommunityMember {} public class student extends CommunityMember {} public class faculty extends Employee {}

Inheritance The superclass should always contain the common and generic instance variables and methods of the subclasses. The subclass should always contain the class specific instance variables and methods. –This may include more specific versions of the methods which reside in the super class.

Inheritance Subclasses –Subclasses are usually bigger and more specific than the super class. –Inherit all public and protected members of the super class even if the subclass does not use them. –Methods and variables in the super class that should not be accessed by a subclass need to be defined as private. –A method in a super class may be overridden in the subclass. –A super class should never access a method or variable in a subclass.

Inheritance Subclasses –Subclasses can access the public, protected, and package access members of it’s super class. Package access is only available if both the super class and the subclass are in the same package. Protected methods and variables can only be accessed by methods of a super class, a subclass, and a class in the same package. Page 668 of Wu states “A protected member is accessible only to the methods that belong to the same class or to the descendant classes. It is inaccessible to the methods of an unrelated class.” This is only true if the unrelated classes are not in the same package as the super and descendant classes.

Inheritance –Subclasses can not access the private members of it’s super class. –A problem with inheritance is that the subclass may inherit methods that it doesn’t need or needs to use differently. The required implementation can be provided by overriding the method in the subclass.

Inheritance Overriding a super class method: –The method in the subclass must have the same signature as the method in the super class. –The subclass version is used when it is mentioned in the subclass or is specifically called in another class using the className.methodName().

Inheritance Direct inheritance occurs when subclass explicitly inherits from a super class. Indirect inheritance occurs when a subclass inherits from a super class two or more levels up in the class hierarchy.

Inheritance When an object of a subclass is instantiated, the superclass’s constructor is called to do any necessary initialization of the superclass instance variables. –This will be done automatically when the subclass constructor is called. When a subclass constructor is called: –First the superclass constructor is executed then the subclass constructor is executed. –Super class constructors are not inherited by subclasses. Subclass constructors can call Super class constructors.

Inheritance To access a method resident in the superclass while in the subclass use super. –super.methodName();

Inheritance Typically there will only be one finalize method in the highest level Super class. –The purpose of a finalize method is to do any necessary clean up when the object is no longer needed. –The finalize method should be defined as protected in the super class. –The subclass can access it using: super.finalize();

Inheritance –If a subclass defines a finalize method, then the last statement of the method should always be: super.finalize();

Inheritance vs. Composition An is a relationship is implemented by inheritance. –A Junior is a Student A has a relationship occurs when a class creates an instance of another class object and this is called composition. –A Junior has a Birthdate. Makes sense –A Student has a Junior. Makes no sense

Inheritance Example: –Animals

Final Methods A method may be defined as final just as a variable may be defined as final. –This means that a subclass is not able to override a final method in the superclass. –Methods that are defined as static or private are instances of final methods.

Final Classes A class may also be defined as final. –Such a class can not be a superclass. –All methods in a class that is defined to be final are also considered final.

Abstract Classes An abstract class is a class that never has an object instantiated from it. –Abstract classes are only used as superclasses. –It’s purpose is to provide an appropriate general superclass from which other classes may inherit. –An abstract class needs to be declared as such: public abstract class className{}

Concrete Classes A concrete class is a class from which objects may be created. –Concrete classes provide specific methods and variables that make it reasonable to instantiate objects. These are the types of classes we have used thus far.

Polymorphism Polymorphism means that different objects respond distinctly to the same message. –Through polymorphism, one method call can cause different actions to occur depending on the type of the object receiving the call.

Example Example: Shape, Point, Circle, Cylinder

Interfaces An Interface is a special kind of class that defines the specifications of a set of methods. –It is a way to obtain abstraction. –It begins with the keyword interface. –It contains a set of public abstract methods or a set of constants to be used in many class definitions. –To use in a class, the class must specify that it implements interfaceName.

Interfaces –An interface is typically used instead of an abstract class when there is no default implementation to inherit. –When a class implements an interface, the same is a relationship is provided by inheritance.

Interfaces Benefits –A class can implement as many interfaces as it needs in addition to extending a class. This is done by: public class className2 implements interfaceName1, interfaceName2, interfaceName3 extends className1 {}

Interfaces Example: –Inventory –Queues