Interfaces.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Object Oriented Programming
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Java Package Advantage of Java Package
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Modern Programming Tools And Techniques-I
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Packages When we name a program , we name it by class name…
Packages, Interfaces & Exception Handling
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Abstract Data Types and Encapsulation Concepts
Object Based Programming
UNIT-3.
Abstract classes and interfaces
Interface.
Java Programming Language
Packages and Interfaces
Interfaces.
Object Oriented Programming in java
Sampath Kumar S Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Applying OO Concepts Using Java
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Constructors, GUI’s(Using Swing) and ActionListner
PACKAGES.
Packages & Random and Math Classes
Abstract classes and interfaces
Final and Abstract Classes
Interfaces,Packages and Threads
C++ Object Oriented 1.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

interfaces

Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface. By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism. Interfaces are designed to support dynamic method resolution at run time.

Defining an interface An interface is defied much like a class. This is a simplified general form of an interface: access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); //… return-type method-nameN(parameter-list); } The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.

An interface is different from a class in several ways, including: An interface cannot be instantiated. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces.

Implementing an interface Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form is: class classname extends superclass implements interface { // class-body } If a class implements more than one interface, the interfaces are separated with a comma.

Differences between abstract classes and interfaces 1) Abstract class can have abstract and non-abstractmethods. Interface can have only abstract methods. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor. 5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class. 6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 7) Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); }

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%). Understanding relationship between classes and interfaces As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.

EG program to demonstrate interface PROG: SeriesDemo The classes that implement an interface not only limited to those methods in an interface. The class can provide whatever additional functionality is desired. Any number of classes can implement an interface. Using interface reference: An interface declaration creates a new reference type. When a class implements an interface, it is adding that interface’s type to its type. Interface reference variable can refer to any object that implements the interface. PROG:SeriesDemo2

IMPLEMENTING MULTIPLE INTERFACES A class can implement more than one interface. Multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class. 

interface IfA { void doSomething(); } interface IfB { void doSomethingElse(); // Implement both IfA and IfB. class MyClass implements IfA, IfB { public void doSomething() { System.out.println("Doing something."); public void doSomethingElse() { System.out.println("Doing something else.");

If a class implements two interfaces that declare the same method, then the same method implementation will be used for both interfaces. This means that only one version of the method is defined by the class. PROG: MultiImpDemo

Constants in Interfaces: The primary purpose of an interface is to declare methods that provide a well defined interface to functionality. An interface can also include variables, but these are not instance variables instead they are implicitly public, final, static and must be initialized. To define a set of shared constants, simply create an interface that contains only those constants without any methods. Each class that needs to access the constants simply “implements” the interface.

// An interface that contains constants // An interface that contains constants. interface IConst { int MIN = 0; int MAX = 10; String ERRORMSG = "Boundary Error"; } // Gain access to the constants by implementing IConst. class IConstDemo implements IConst { public static void main(String[] args) { int[] nums = new int[MAX]; for(int i=MIN; i < (MAX + 1); i++) { if(i >= MAX) System.out.println(ERRORMSG); else { nums[i] = i; System.out.println(nums[i] + " ");

INTERFACES CAN BE EXTENDED: One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain. PROG: IFExtend

Nested Interfaces An interface can be declared a member of a class or another interface. Such an interface is called a member interface or a nested interface. A nested interface can be declared as public, private, or protected. This differs from a top-level interface, which must either be declared as public or use the default access level. When a nested interface is used outside of its enclosing scope, it must be qualified by the name of the class or interface of which it is a member. Thus, outside of the class or interface in which a nested interface is declared, its name must be fully qualified. PROG: NestedIFDemo

PACKAGES

PACKAGE A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Advantage of Java Package 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision.

Defining a Package: To create a package is quite easy: simply include a package command as the first statement in a Java source file. This is the general form of the package statement: package pkg; Here, pkg is the name of the package. For example, the following statement creates a package called MyPackage: package MyPackage;

You can create a hierarchy of packages. package pkg1[.pkg2[.pkg3]]; Eg : package java.awt.image;

A simple package example. Save the following file as A.java in a folder called pack. package pack; public class A { public void msg() { System.out.println("Hello");} }

Save the following file as B.java in a folder called mypack. package mypack; import pack.*; class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); }

Assume that pack and mypack folders are in the directory E:/java. compile: E:/java>javac mypack/B.java Running: E:/java>java mypack/B Hello

PACKAGES AND MEMBER ACCESS: Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods. Packages act as containers for classes and other subordinate packages. Classes act as containers for data and code. The class is Java’s smallest unit of abstraction. Because of the interplay between classes and packages, Java addresses four categories of visibility for class members: • Subclasses in the same package • Non-subclasses in the same package • Subclasses in different packages • Classes that are neither in the same package nor subclasses

CLASS MEMBER ACCESS Private Member Default Protected Member Public Member Visible within same class YES Visible within same package by subclass NO Visible within same package by non-subclass Visible within different package by subclass Visible within different by non-subclass

A package access example:

IMPORTING PACKAGES: Java includes the import statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name. This is the general form of the import statement: import pkg1 .pkg2.classname | *; Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). Eg: import mypack.MyClass; import mypack.*; * indicates that the Java compiler should import the entire package.

All of the standard Java classes included with Java are stored in a package called java. The basic language functions are stored in a package inside of the java package called java.lang. It must be emphasized that the import statement is optional. Any place you use a class name, you can use its fully qualified name, which includes its full package hierarchy. For example, this fragment uses an import statement: import java.util.*; class MyDate extends Date { } The same example without the import statement looks like this: class MyDate extends java.util.Date {

JAVA’S Standard packages: Sub package Description java.lang Contains a large number of general –purpose classes java.io Contains the I/O classes java.net Contains those classes that support networking java.applet Contains classes for creating applets java.awt Contains classes that support the Abstract Window Toolkit java.util Contains various utility classes, plus the Collections Framework

STATIC IMPORT: Java includes a feature called static import that expands the capabilities of the import keyword. By following import with the keyword static, an import statement can be used to import the static members of a class or interface. When using static import, it is possible to refer to static members directly by their names, without having to qualify them with the name of their class.

// Compute the hypotenuse of a right triangle. import java. lang. Math // Compute the hypotenuse of a right triangle. import java.lang.Math.sqrt; import java.lang.Math.pow; class Hypot { public static void main(String args[]) { double side1, side2; double hypot; side1 = 3.0; side2 = 4.0; // Notice how sqrt() and pow() must be qualified by // their class name, which is Math. hypot = Math.sqrt(Math.pow(side1, 2) +Math.pow(side2, 2)); System.out.println("Given sides of lengths " +side1 + " and " + side2 +" the hypotenuse is " +hypot); }

// Compute the hypotenuse of a right triangle. import static java.lang.Math.sqrt; import static java.lang.Math.pow; class Hypot { public static void main(String args[]) { double side1, side2; double hypot; side1 = 3.0; side2 = 4.0; // Notice how sqrt() and pow() must be qualified by // their class name, which is Math. hypot = sqrt(pow(side1, 2) +pow(side2, 2)); System.out.println("Given sides of lengths " +side1 + " and “ + side2 +" the hypotenuse is " +hypot); }

ASSignment on Thursday- 03/03/2016 1. Write a short notes on a)abstract classes b)final keyword. 2. Define an interface? Write a program which illustrates the design and implementation of an interface? 3. a) What is a package? Explain member access privileges. b) Write a sample program to illustrate packages 4. Explain about static import.