Page 286 - PowerPoint Presentation
P. 286

CAVITE STATE UNIVERSITY
                               TRECE MARTIRES CITY CAMPUS
                               Department of Information Technology            DCIT 111 - Advanced Programming

               Multiple Inheritance in Java by Interface
                       If a class implements multiple interfaces, or an interface extends multiple interfaces, it
               is known as multiple inheritance.

                        Interface        Interface                  Interface         Interface
                               implements                                     extends

                                  Class                                       Interface
                                                Multiple Inheritance in Java

                  interface Printable {
                        void print();
                  }                                        public class TestInterface3 {
                  interface Showable {                     public static void main(String[]args) {
                        void show();                              A4 obj = new A4();
                  }                                               obj.print();

                  class A4 implements Printable, Showable {       obj.show();
                        public void print() {              }
                               System.out.println(“Hello”);   }
                        }
                        public void print() {
                               System.out.println(“Welcome”);
                        }
                  }

               Difference between Abstract Class and Interface
                       Abstract  class  and  interface  both  are  used  to  achieved  abstraction  where  we  can
               declare the abstract methods. Abstract class and interface both can’t be instantiated.
                       But there are many differences between abstract class and interface that are given
               below.
                              Abstract Class                                   Interface
                 Abstract class can have abstract and non-     Interface can have only abstract methods.
                              abstract method.                Since Java 8, it can have default and static
                                                                            methods also.
                      Abstract doesn’t support multiple         Interface supports multiple inheritance.
                                inheritance.
                   Abstract class can have final, non-final,   Interface has only static and final variables.
                       static and non-static variables.
                       Abstract class can provide the          Interface can’t provide the implementation
                        implementation of interface.                       of abstract class.
                  The abstract keyword is used to declare       The interface keyword is used to declare
                               abstract class.                                 interface.
                  An abstraction class can extend another     An interface class can extend another Java
                  Java class and implement multiple Java                    interface only.
                                 interfaces.
                  An abstract class can be extended using       An interface class can be implemented
                             keyword ‘extends’.                       using keyword ‘implements’.
                    A Java abstract class can have class       Members of a Java interface are public by
                    members like private, protected, etc.                       default.
                  Example:     public abstract class Shape {    Example:    public interface Drawable {
                                       public abstract void draw(); }                           void draw(); }
               Simply, abstract class achieves partial abstraction (0% to 100%) whereas interface achieves
               fully abstraction (100%).



                                                                                                            62
   281   282   283   284   285   286   287   288   289   290   291