The Java Alternative to Multiple Inheritance

Slides:



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

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Chapter 10: Introduction to Inheritance
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
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.
Copyright © Curt Hill Sounds, Resource Packs, JSON What more would you want?
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique.
Copyright © Curt Hill Inheritance in C++ How to do it.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Inheritance in C++ How to do it Copyright © Curt Hill
Modern Programming Tools And Techniques-I
More About Java and Java How to Program By Deitel & Deitel.
Advanced Programming in Java
Advanced Programming in Java
A Review or Brief Introduction
Inheritance-Basics.
Inheritance and Polymorphism
Interface.
Using SLK and Flex++ Followed by a Demo
Java Import Statement Copyright © Curt Hill
Inheritance and Polymorphism
Copyright © by Curt Hill
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Types of Programming Languages
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Java Programming Language
IFS410: Advanced Analysis and Design
Constants in Java Why and How Copyright © Curt Hill.
Interfaces.
Arrays in Java What, why and how Copyright Curt Hill.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
The Diamond Problem Its Solution Copyright © 2017 Curt Hill.
Advanced Programming in Java
Java – Inheritance.
Advanced Programming in Java
Multiple Inheritance in C++
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Review of Previous Lesson
Enhanced Entity-Relationship (EER) Modeling
Inheritance Lakshmish Ramaswamy.
Chapter 7 Inheritance.
Computer Science II for Majors
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

The Java Alternative to Multiple Inheritance Interfaces The Java Alternative to Multiple Inheritance Copyright © 1998-2017 Curt Hill

Introduction There are two similar but different useful techniques in Java Derivation Interfaces The good Java programmer needs both Derivation is widely used to add specialization to classes Interfaces are required for Layout Managers, among others Copyright © 1998-2017 Curt Hill

Derivation & Interfaces Derivation allows a new class to inherit properties and methods from an ancestor Inheritance is discussed at length in a previous presentation Interfaces allow a class to implement common features without inheriting code or data Used to replace multiple inheritance Copyright © 1998-2017 Curt Hill

Multiple inheritance C++ and Eiffel and other languages allow The iostreams are classic examples The formatting comes from one class The I/O from another Works very nicely SmallTalk had it then removed it Unfortunately there are problems Recall the person inheritance hierarchy Copyright © 1998-2017 Curt Hill

person set_name employee student set_wage set_major grad set_degree Copyright © 1998-2017 Curt Hill

Multiple Inheritance Problems Suppose that we wanted a student employee We could then derive a class from both student and employee The class would have two name properties One from person through student One from person through employee How do we distinguish between the two Copyright © 1998-2017 Curt Hill

Multiple Inheritance Problems like this have not been resolved well in any programming language Eiffel comes closest Thus Java. C# and Smalltalk disallow multiple inheritance Java and C# substitute the Interface This gives many of the advantages None of the problems Copyright © 1998-2017 Curt Hill

Interfaces An interface is somewhat like a pure abstract base class It defines no code and only constant data It defines what methods are needed, without giving any indication at all about the implementation Provides signatures only If a class claims that it implements an interface it must implement these defined methods Otherwise compile errors Copyright © 1998-2017 Curt Hill

Using an interface Using an interface requires the implements reserved words: class xyz implements abc {…} abc must be a defined interface A class may implement several interfaces but only be derived from one class Derivation uses the extend reserved word Copyright © 1998-2017 Curt Hill

Defining an interface Similar to a class: public interface Employee { void set_wage (double w); } Notice this does not set the wage field This is only the signature – no code Multiple methods are possible An interface may also extend another interface Copyright © 1998-2017 Curt Hill

Creating the employee Here we have both derivation and interface class employee extends person implements Employee{ class stu_emp extends student implements Employee{ Either one will be abstract if it does not implement set_wage An abstract class may not be instantiated Like an abstract base class Useful but not as common as instantiable classes Copyright © 1998-2017 Curt Hill

Why use interfaces? Several common examples that we will use Layout Managers Event handlers in AWT 1.1 (or later) event model Image handlers Especially in Applets Multi-threaded classes Each of these that we need will be considered in a separate presentation Copyright © 1998-2017 Curt Hill

Abstract Base Class or Interface? An interface tells us all that we need to know: the method signature There is no common code or data An abstract base class usually adds common code and data A class may only have one base class, but many interfaces Copyright © 1998-2017 Curt Hill

Finally We have looked at this in a somewhat formal sense What we next need is both the usage of an interface and later the definition of a worth while interface Copyright © 1998-2017 Curt Hill