Interfaces.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Multiple Choice Solutions True/False a c b e d   T F.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
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.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
OOP: Encapsulation &Abstraction
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Final and Abstract Classes
Inheritance and Polymorphism
Interface.
UML Class Diagram: class Rectangle
Abstract classes and interfaces
Interface.
Enumerations & Annotations
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
Conditional Statements
Abstract Classes AKEEL AHMED.
Packages and Interfaces
Advanced Java Programming
Sampath Kumar S Assistant Professor, SECE
Advanced Programming in Java
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Advanced Programming in Java
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
CIS 199 Final Review.
Abstract classes and interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Interfaces,Packages and Threads
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Interfaces

interface An interface in java is a blueprint of a class. It has static constants and abstract methods. It cannot be instantiated just like abstract class. It is used to achieve abstraction and multiple inheritance in Java. Since Java 8, interface can have default and static methods.

Properties of interfaces The interface keyword is used to declare an interface. Interfaces have the following properties: An interface is implicitly abstract. We do not need to use the abstract keyword when declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public. Variable in an interface are implicitly public, static & final (and have to be assigned values there).

interface interface fields are public, static and final by default, and methods are public and abstract.

Implementing interfaces When a class implements an interface, then it has to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.

Example (implementing interface)

Example (members accessibility)

Example (Bank)

Example (Shape)

interface vs. class An interface is similar to a class in the following ways: An interface can contain any number of methods. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. The byte-code of an interface appears in a .class file. Interfaces appear in packages, and their corresponding byte- code file must be in a directory structure that matches the package name.

interface vs. class An interface is different from a class in several ways, including: We cannot instantiate an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.

Example (multiple inheritance using interfaces)

Example (inheritance in interfaces)

Default method in interface

Default methods Default methods are defined inside the interface and tagged with default are known as default methods. These methods are non-abstract methods. Since Java 8, we can have method body in interface. But we need to make it default.

Example

Example (multiple inheritance using interfaces with default methods) In order to remove ambiguity, override the method in the class as: public void area() { Shape1.super.area(); //to call method of Shape1 }

static method in interface

static method Since Java 8, you can define static methods in interfaces. Java interface static method is part of interface, we can’t use/access it using class objects. Java interface static method helps us in providing security by not allowing implementation classes to override them.

Example

Example (multiple inheritance using interfaces with static methods)