Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Object Oriented Programming
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.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10: Introduction to Inheritance
Inheritance “a mechanism for propagating properties (attributes & methods) of superclasses to subclasses.”
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
UML Class Diagram: class Rectangle
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
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.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
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
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
© 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 Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Section 11.1 Class Variables and Methods
UML Class Diagram: class Rectangle
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
MSIS 670 Object-Oriented Software Engineering
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Advanced Programming in Java
OOP Aga private institute for computer science 5th grade
Inheritance Lakshmish Ramaswamy.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Interface & Abstract Class

Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part. Methods be implemented in the subclasses that use them.

Multiple Inheritance Using Interface The interface construct in Java is used with single inheritance to provide some form of multiple inheritance.

Multiple Inheritance Using Interface In order that a sales manager has the ability to manage, we add to the SalesManager class, appropriate behavior in an interface which is then inherited by the SalesManager class. We shall call that interface Manage as follows:

Multiple Inheritance Using Interface

In using the Manage interface, the subclass SalesManager must implement the abstract methods of the interface. This is reflected in the class declaration of SalesManager:

Multiple Inheritance Using Interface A sales manager is basically a salesperson with an additional behavior to authorize payments (via the authorize() method).

Attributes in an Interface Data attributes declared in an interface construct are always static and final. They are static as there can only be one copy of the data available and final since they are not modifiable. By declaring data attributes in an interface, constant declarations for use in methods is possible. Constants are names for values with a specific meaning.

Attributes in an Interface As all data attributes are implicitly declared as static and final in an interface definition, these keywords need not precede their declaration:

Methods in an Interface All methods in an interface are abstract methods and any class that uses the interface must provide an implementation for them. An interface does not have to explicitly declare its methods abstract using the keyword abstract. Interface methods are always public, and the access modifier public keyword is not required since it is implied in the interface declaration. In contrast with data attributes in an interface, methods may not be static since static methods, being class specific, are never abstract.

Abstract Class & Interface A class implementing an interface must implement all the abstract methods declared in an interface. The class is considered as an abstract class and must be declared using the abstract keyword.

Abstract Class & Interface

Abstract ClassInterface May have some methods declared abstract. Can only have abstract methods. May have protected properties and static methods. Can only have public methods with no implementation. May have final and nonfinal data attributes. Limited to only constants.

Abstract Class & Interface An abstract class can enhance inheritance as some or all parts of the class can be implemented and inherited by subclasses. An interface, on the other hand, is generally used for achieving multiple inheritance in Java. An abstract class cannot be used to instantiate objects since it may contain parts that are not implemented.

Extending Interface A subclass of a class that implements an interface also inherit the methods of the interface.

Extending Interface

Limitation of interface for Multiple Inheritance Although the interface feature in Java provides an alternative solution to achieving multiple inheritance in class hierarchies, it has its limitations: a. An interface does not provide a natural means of realizing multiple inheritance in situations where there is no inheritance conflict. b. While the principal reason for inheritance is code reusability, the interfacefacility does not encourage code reuse since duplication of code is inevitable.