Interfaces<br />DhrubojyotiKayal<br />
Some classes are better of just defining common behavior<br />You also want to prevent possibility of creating instances o...
A class containing abstract methods is called an abstract class. <br />If a class contains one or more abstract methods, t...
You cannot create an object of a class declared abstract<br />If you inherit from an abstract class and you want to make o...
Create an abstract Java class with one abstract method and another non abstract method<br />Try to create an instance of t...
The interface keyword produces a completely abstract class, one that provides no implementation at all. <br />It allows th...
To create an interface, use the interface keyword instead of the class keyword <br />Same access specifiers work as with c...
public interface Singable {<br />	public void sing();<br />}<br />public class Parrot implements Singable {<br />	public v...
Design an interface to ensure that Shape class and its children– Rectangle, Square can be drawn<br />Design an interface t...
Just like class it is possible to extend an interface<br />public interface A {<br />	public int a();<br />}<br />public i...
Q&A<br />
Upcoming SlideShare
Loading in …5
×

15 interfaces

812 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
812
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

15 interfaces

  1. 1. Interfaces<br />DhrubojyotiKayal<br />
  2. 2. Some classes are better of just defining common behavior<br />You also want to prevent possibility of creating instances of this class<br />Make the class abstract<br />public class Shape{<br /> public abstract void draw() {<br /> }<br />}<br />Abstract class<br />
  3. 3. A class containing abstract methods is called an abstract class. <br />If a class contains one or more abstract methods, the class itself must be qualified as abstract <br />public abstract class Shape {<br /> public abstract void draw();<br />}<br />Abstract Class<br />
  4. 4. You cannot create an object of a class declared abstract<br />If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class <br />If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword. <br />It’s possible to make a class abstract without including any abstract methods <br />You can have methods in an abstract class which have reusable code and they themselves are not declared abstract<br />Abstract Class<br />
  5. 5. Create an abstract Java class with one abstract method and another non abstract method<br />Try to create an instance of the abstract class<br />Excerise<br />
  6. 6. The interface keyword produces a completely abstract class, one that provides no implementation at all. <br />It allows the creator to determine method names, argument lists, and return types, but no method bodies. <br />An interface provides only a form, but no implementation. <br />An interface says, "All classes that implement this particular interface will look like this." <br />Any code that uses a particular interface knows what methods might be called for that interface, and that’s all. <br />So the interface is used to establish a "protocol" between classes <br />Interfaces<br />
  7. 7. To create an interface, use the interface keyword instead of the class keyword <br />Same access specifiers work as with classes, but mostly public<br />An interface can also contain fields, but these are implicitly static and final. <br />To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword <br />Interfaces<br />
  8. 8. public interface Singable {<br /> public void sing();<br />}<br />public class Parrot implements Singable {<br /> public void sing() {<br /> }<br /> public void fly() {<br /> }<br />}<br />Interface in Action<br />
  9. 9. Design an interface to ensure that Shape class and its children– Rectangle, Square can be drawn<br />Design an interface to ensure that Shape class and its children – Rectangle, Square can be filled with color<br />Now write a Test class and create two references one each of the children and assign them to a Shape variable. Invoke the different methods <br />Now try assigning the Shape references to the interface variables. Try invoking the methods <br />Exercise<br />
  10. 10. Just like class it is possible to extend an interface<br />public interface A {<br /> public int a();<br />}<br />public interface B extends a {<br />}<br />Extending an interface<br />
  11. 11. Q&A<br />

×