Inheritance Lakshmish Ramaswamy.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
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.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
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.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
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)
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Introduction to Object-Oriented Programming Lesson 2.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
ISBN Chapter 12 Support for Object-Oriented Programming.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Advanced Programming in Java
Advanced Programming in Java
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 12 Inheritance.
Object-oriented Programming in Java
Inheritance and Polymorphism
Inheritance AKEEL AHMED.
Packages, Interfaces & Exception Handling
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Interfaces.
Object-Oriented Programming: Polymorphism
Interface.
Extending Classes.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
CSE 1030: Implementing GUI Mark Shtern.
Java – Inheritance.
Advanced Programming in Java
COMPUTER 2430 Object Oriented Programming and Data Structures I
Fundaments of Game Design
Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Inheritance Lakshmish Ramaswamy.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Inheritance Lakshmish Ramaswamy

Multiple Inheritance A class being derived from more than one base class Ex: A StudentEmployee Class being derived both from Student and Employee Some OO languages (ex: C++) support multiple inheritance Java does not allow multiple inheritance

Issues with Multiple Inheritance Does StudentEmployee get two copies of name, address and phone number? If StudentEmployee does not override toString method, which implementation would be used? If a method is declared to be final in one base class and not in the other, would the StudentEmployee be allowed to override the method?

Interface Alternative to multiple inheritance The ultimate abstract class Consists only of public abstract methods & public static final fields No implementation of any method Class providing definitions of all abstract methods is said to implement the interface Multiple interfaces do not suffer from same problems as multiple inheritance Class can implement multiple interfaces but extend from a single class

Specifying Interface Similar to a class declaration but uses the keyword interface Lists methods that need to be implemented Class implementing an interface should do two things Declare that it implements the interface using “implements” Provide implementations of at least one method declared in interface A method implementing an interface may remain abstract

Example of Interface

Example of Implementing Class

Multiple Interfaces Java allows multiple interfaces List all the interfaces a class is implementing Provide implementations of all methods in all interfaces public class X implements a, b { … }

Interfaces as Abstract Classes IS-A relationship holds instanceof operator can be used to test type compatibility Implementing class cannot reduce the visibility of methods Implementing class may not add checked exceptions to throws list In case of multiple interfaces implementation can throw exception that is listed in all interfaces

Interfaces as Abstract Classes Class implementing an interface should override with the exact signature Cannot implement two interfaces with same signature but different return types Class that partially implements an interface should be declared abstract Interfaces can extend other interfaces

Fundamental Inheritance in Java If class does not explicitly extend another class, it implicitly extends Object class Object contains several methods Object is not abstract class All methods are implemented toString, equals and hashCode are some of the important methods