Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Interfaces & Abstract Classes


Published on

Published in: Technology, Education
  • Be the first to comment

Interfaces & Abstract Classes

  1. 1. Interfaces & Abstract classes
  2. 2. Interface <ul><li>Java’s interfaces are an improvement over multiple inheritance mechanism of C++. </li></ul><ul><li>While designing a software system in java, Interfaces help us defining the interfaces between the components. </li></ul><ul><li>Interfaces help us achieve a special kind of multiple inheritance : multiple inheritance of interfaces without multiple inheritance of implementation. </li></ul>
  3. 3. Which is a better design pattern? <ul><li>Multiple inheritance of interfaces or Multiple inheritance of classes (implementations). </li></ul><ul><li>Interfaces solve Diamond Problem with traditional multiple inheritance. </li></ul>
  4. 4. <ul><li>abstract class Animal </li></ul><ul><li>{ abstract void talk(); } </li></ul><ul><li>class Frog extends Animal </li></ul><ul><li>{ </li></ul><ul><li>void talk() </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Ribit, ribit.&quot;); } </li></ul><ul><li>} </li></ul><ul><li>class Dinosaur extends Animal </li></ul><ul><li>{ </li></ul><ul><li>void talk() </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Oh I'm a dinosaur and I'm OK...&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>// This won't compile, of course, because //Java only supports single inheritance. </li></ul><ul><li>class Frogosaur extends Frog, Dinosaur { } </li></ul><ul><li>Animal animal = new Frogosaur();; </li></ul>When we think of objects, We assume it as programmatic representations of real-world entities. Confused, isn’t it ?
  5. 5. Think <ul><li>Animal would have declared a public instance variable, which Frogosaur would then have inherited from both Dinosaur and Frog. When referring to this variable in a Frogosaur object, which copy of the variable -- Frog's or Dinosaur's -- would have been selected? </li></ul><ul><li>We solve all these ambiguities in software design using interfaces in Java. Through interfaces, Java allows multiple inheritance of interfaces but not of implementations. Implementation, which includes instance variables and method implementations, is always singly inherited. </li></ul>
  6. 6. Interfaces & Polymorphism <ul><li>We learned, Interface is the Java’s way to solve diamond problem. But is this the only benefit?? </li></ul><ul><li>Interface also lets us take greater advantage of polymorphism in our designs, which in turn helps us make our software more flexible. </li></ul>
  7. 7. What’s this Polymorphism? <ul><li>A greek word meaning many shapes. In context of Java, a class has many forms: that of the class and any of its subclasses. An Animal, for example, can look like a Dog or a Cat or any other subclass of Animal. </li></ul><ul><li>It also means using a superclass variable to refer to a subclass object. For Ex:- </li></ul>abstract class Animal { abstract void talk(); } class Dog extends Animal { void talk() { System.out.println(&quot;Woof!&quot;); } } class Cat extends Animal { void talk() { System.out.println(&quot;Meow.&quot;); } } class Interrogator { static void makeItTalk(Animal subject) {; } }
  8. 8. Polymorphism contd. <ul><li>Types : 2 </li></ul><ul><li>1. Compile-time Polymorphism </li></ul><ul><li>2. Runtime Polymorphism </li></ul><ul><li>Think, In the previous ex. the compiler knows only : </li></ul><ul><li>an object of some Animal subclass will be made available to makeItTalk(). </li></ul><ul><li>It means, JVM will decide at runtime which method to invoke based on the class. </li></ul><ul><li>How is our program going to be flexible this way? </li></ul><ul><li>Think of adding some more classes as the one below ….. </li></ul>class Bird extends Animal { void talk() { System.out.println(&quot;Tweet, tweet!&quot;); } } Do we need to change the makeItTalk() method??
  9. 9. Polymorphism contd. <ul><li>Getting more Polymorphism: </li></ul><ul><li>Interfaces give us more polymorphism than singly inherited families of classes, because with interfaces we don't have to make everything fit into one family of classes. </li></ul>interface Talkative { void talk(); } abstract class Animal implements Talkative { abstract public void talk(); } class Dog extends Animal { public void talk() { System.out.println(&quot;Woof!&quot;); } } class Cat extends Animal { public void talk() { System.out.println(&quot;Meow.&quot;); } } class Interrogator { static void makeItTalk(Talkative subject) {; } }
  10. 10. Polymorphism contd. <ul><li>Here is the magic!! </li></ul>class Clock { } class CuckooClock implements Talkative { public void talk() { System.out.println(&quot;Cuckoo, cuckoo!&quot;); } } class Test { public static void main(String[] args) { CuckooClock cc = new CuckooClock(); Interrogator.makeItTalk(cc); } }
  11. 11. Polymorphism contd. <ul><li>Conclusion :- </li></ul>With single inheritance only, we'd either have to somehow fit CuckooClock into the Animal family, or not use polymorphism. With interfaces, any class in any family can implement Talkative and be passed to makeItTalk(). This is why we say interfaces provide more polymorphism than we can get with singly inherited families of classes.
  12. 12. Using Interfaces <ul><li>Variables declared in the interfaces are implicitly public, static & final. </li></ul><ul><li>Methods declared in the interfaces are implicitly public & abstract. </li></ul><ul><li>Following modifiers can’t be applied to the methods declared in an interface :- </li></ul><ul><li>private, protected, static, final, synchronized </li></ul><ul><li>Variables declared in an interface can’t be private or protected. </li></ul><ul><li>An interface can extend one or more interfaces. </li></ul><ul><li>One or more interfaces can be implemented in a class. </li></ul>