Interfaces in Java

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

By: vanithaarivu (6 month(s) ago)

hai nice ppt could you send this to vanithaarivu@gmail.com

By: karthijyoe (8 month(s) ago)

excellent ppt can u send it to karthickajyoe@gmail.com

By: stanleynick3 (9 month(s) ago)

I am in a desperate need of this ppt. So can u Please send this ppt to my mail.id. stanleynick.4@gmail.com.

By: vidyasagar1 (13 month(s) ago)

Kindly send this to my mail id yvschowdary21@gmail.com. Thanks in advance!

By: rajeshsri (17 month(s) ago)

nice presentation with excellent definition

See all

Presentation Transcript

Interfaces in : 

Interfaces in

Why use interfaces? : 

Why use interfaces? Java does not have multiple inheritance Inheritance gives us two things : - Code reuse - Ability to represent the object polymorphically Interface support the concept of multiple inheritance

Defining interfaces : 

Defining interfaces An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. public interface MyStack { public int size(); public boolean isEmpty(); public Object top(); public void push(Objectelt); public Object pop();

About interfaces : 

About interfaces An interface specifies a list of one or more methods, giving only their signatures, but no code A class implements an interface if it supplies code for all methods of that interface public interface MyStack { public boolean isEmpty(); } Class stacks implements Mystack{ isEmpty(S): return (top < 0); }

Extending interfaces : 

Extending interfaces An interface can be subinterfaced from other interfaces. Subinterface will inherit all members of superinterface. interface name2 extends name1 { body of name2 }

Various forms of interface implementation : 

Various forms of interface implementation

Interface Example - Area : 

Interface Example - Area public interface Area { final float PI = 3.1425926F; //static and final constant. float compute (float x, float y); } public class Circle implements Area { public float compute (float x, float y) { return (PI*x*x); } } public class Rectangle implements Area { public float compute (float x, float y) { return (x*y); } } implements

Example contd.. : 

Example contd.. Class InterfaceTest { public static void main (String args[ ]) { Rectangle rect = new Rectangle(); Circle cir =new Circle(); Area area; area = rect; system.out.println(“area of rectangle = “ + area.compute(10,20)); area = cir; system.out.println(“area of circle = “ + area.compute(10,10)); implements Output: area of rectangle = 200 area of circle = 314

Classes Interfaces : 

Classes Interfaces A superclass provides a secondary data type to objects of its subclasses. An abstract class cannot be instantiated. An interface provides a secondary data type to objects of classes that implement that interface. An interface cannot be instantiated. Similarities

Classes Interfaces : 

Classes Interfaces A concrete subclass of an abstract class must define all the inherited abstract methods. A class can extend another class. A subclass can add methods and override some of its superclass’s methods. A concrete class that implements an interface must define all the methods specified by the interface. An interface can extend another interface (called its superinterface) by adding declarations of abstract methods. Similarities

Classes Interfaces : 

Classes Interfaces A class can extend only one class. A class can have fields. A class defines its own constructors (or gets a default constructor). A class can implement any number of interfaces. An interface cannot have fields. An interface has no constructors. Differences

Classes Interfaces : 

Classes Interfaces A concrete class has all its methods defined. An abstract class usually has one or more abstract methods. Every class is a part of a hierarchy of classes with Object at the top. All methods declared in an interface are abstract. An interface may belong to a small hierarchy of interfaces, but this is not as common. Differences

Advantages of Interfaces : 

Advantages of Interfaces Provide a standard set of methods for a group of classes. This is Java’s implementation of an Abstract Data Type (ADT) Objects of these classes can be accessed by the standard set of methods without considering about their location in class hierarchy. Support selective multiple inheritance. e.g. java.util.LinkedList

Slide 14: 

THANK YOU!!!