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.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Lecture 2: Object Oriented Programming I
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
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.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
Classes (Part 1) Lecture 3
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
Final and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
CompSci 230 S Programming Techniques
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Object Based Programming
Chapter 9 Inheritance and Polymorphism
Lecture 4: Interface Design
More inheritance, Abstract Classes and Interfaces
Abstract classes and interfaces
Interface.
CMSC 202 Interfaces.
Extending Classes.
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Packages and Interfaces
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Sampath Kumar S Assistant Professor, SECE
METHOD OVERRIDING in JAVA
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
CMSC 202 Interfaces.
Fundaments of Game Design
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
Inheritance Lakshmish Ramaswamy.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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 should do, without specifying how they should do it. It’s not a class but a set of requirements for classes. In interfaces only abstract methods are allowed and method body is implemented in Subclass. Interface is used to achieve fully abstraction and multiple inheritance in Java. Instance variable cannot created just like abstract class. Visit for more Learning Resources

Relationship between class and interfaces

Interface declarations The declaration consists of a keyword interface, its name, and the members The Java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members. Interface declaration is similar to classes, interfaces is having three types of members constants (fields) methods nested classes and interfaces

Interface member – constants An interface can define named constants, which are public, static and final (these modifiers are omitted by convention) automatically. Interfaces never contain instant fields. All the named constants MUST be initialized An example interface Interface Verbose { int SILENT = 0; int TERSE = 1; int NORMAL = 2; int VERBOSE = 3; void setVerbosity (int level); int getVerbosity(); }

Interface member – methods They are implicitly abstract. So every method declaration consists of the method header and a semicolon. They are indirectly public. No other types of access modifiers are allowed. methods can’t be final, nor static

Modifiers of interfaces itself An interface can have different modifiers as follows public/package(default) abstract all interfaces are implicitly abstract omitted by convention

Example for interface interface printable { void print(); } class A implements printable { public void print() System.out.println("Hello"); public static void main(String args[]){ A obj = new A(); obj.print(); } }

EXAMPLE interface Inter { int a=20; void print(); } class main1 implements Inter public void print() System.out.println("this is my interface"); public static void main(String [] args) main1 m = new main1(); //Inter i= new Inter(); error //Inter i= new main1(); //i.print(); m.print(); System.out.println(Inter.a*a); Output: this is my interface 400

To implement interfaces in a class Two steps to implement an interface in class 1. declare that the class intends to implement the given interface by using the implements keyword class Employee implements Comparable { . . . } 2. supply definitions for all methods in the interface public int compareTo(Object otherObject) { Employee other = (Employee) otherObject; if (salary < other.salary) return -1; if (salary > other.salary) return 1; return 0; } note: in the Comparable interface declaration, the method compareTo() is public implicitly but this modifier is omitted. But in the Employee class design, you cannot omit the public modifier, otherwise, it will be assumed to have package accessibility If a class leaves any method of the interface undefined, the class becomes abstract class and must be declared abstract A single class can implement multiple interfaces. Just separate the interface names by comma class Employee implements Comparable, Cloneable {. . .}

Instantiation properties of interfaces Interfaces are not classes. You can never use the new operator to instantiate an interface. public interface Comparable { . . . } Comparable x = new Comparable( ); You can still declare interface variables Comparable x; but they must refer to an object of a class that implements the interface class Employee implements Comparable { . . . } x = new Employee( );

Extending interfaces Interfaces support multiple inheritance – an interface can extend more than one interface Superinterfaces and subinterfaces Example public interface SerializableRunnable extends java.io.Serializable, Runnable { . . . }

Extending interfaces – about constants (1) An extended interface inherits all the constants from its superinterfaces when the subinterface inherits more than one constants with the same name, or the subinterface and superinterface contain constants with the same name — always use sufficient enough information to refer to the target constants

When an interface inherits two or more constants with the same name In the subinterface, explicitly use the superinterface name to refer to the constant of that superinterface E.g. interface A { int val = 1; } interface B { int val = 2; interface C extends A, B { System.out.println(“A.val = “+ A.val); System.out.println(“B.val = “+ B.val);

If a superinterface and a subinterface contain two constants with the same name, then the one belonging to the superinterface is hidden in the subinterface access the subinterface-version constants by directly using its name access the superinterface-version constants by using the superinterface name followed by a dot and then the constant name E.g interface X { int val = 1; } interface Y extends X{ int val = 2; int sum = val + X.val; } outside the subinterface and the superinterface you can access both of the constants by explicitly giving the interface name. E.g. in previous example, use Y.val and Y.sum to access constants val and sum of interface Y, and use X.val to access constant val of interface X. Y‘s val X’s val

When a superinterface and a subinterface contain two constants with the same name, and a class implements the subinterface the class inherits the subinterface-version constants as its static fields. Their access follow the rule of class’s static fields access. E.g class Z implements Y { } //inside the class System.out.println(“Z.val:“+val); //Z.val = 2 //outside the class System.out.println(“Z.val:“+Z.val); //Z.val = 2 object reference can be used to access the constants subinterface-version constants are accessed by using the object reference followed by a dot followed by the constant name superinterface-version constants are accessed by explicit casting E.g. Z v = new Z( ); System.out.print( “v.val = “ + v.val +“, ((Y)v).val = “ + ((Y)v).val +“, ((X)v).val = “ + ((X)v).val ); output: v.val = 2, ((Y)v).val = 2, ((X)v).val = 1

Extending interfaces – about methods If a declared method in a subinterface has the same signature as an inherited method and the same return type, then the new declaration overrides the inherited method in its superinterface. If the only difference is in the return type, then there will be a compile-time error An interface can inherit more than one methods with the same signature and return type. A class can implement different interfaces containing methods with the same signature and return type. Overriding in interfaces has NO question of ambiguity. The real behavior is ultimately decided by the implementation in the class implementing them. The real issue is whether a single implementation can honor all the contracts implied by that method in different interfaces Methods with same name but different parameter lists are overloaded

interface Printable{   void print();   }      interface Showable{   void show();   class A implements Printable,Showable {   public void print() { System.out.println("Hello"); public void show() System.out.println("Welcome"); public static void main(String args[]) A obj = new A();   obj.print();   obj.show();    }  

Multiple inheritance using interface

Multiple interface public interface Inherit_Multiple { String str1 = "This "; } interface Interface2 { String str2 = "is "; interface Interface3 { String str3 = "Java "; interface Interface4 { String str4 = "World !!!"; class SubClass implements Inherit_Multiple, Interface2, Interface3, Interface4 { String str; SubClass() { str = str1.concat(str2).concat(str3).concat(str4); void display() System.out.println(str); class MainClass { public static void main(String args[]) { SubClass obj = new SubClass(); obj.display(); For more detail contact us