Page 284 - PowerPoint Presentation
P. 284

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

                  class Honda extends Bike {    // creating a Child class which inherits Abstract class
                        void run() {   // this is not an abstract method

                               System.out.println(“running safely”);
                        }
                  }
                  public class TestAbstraction3 {     // creating a Test class which calls abstract and non
                  abstract methods
                        public static void main(String[]args) {
                        Bike Bike = new Honda();
                        Bike.run();
                        Bike.changeGear();
                  }                                           RULE: if there is an abstract method in a class,
                  }                                                  that class must be abstract.

                       A Java abstract class is a class which cannot be instantiated, meaning you cannot
               create new instances of an abstract class. The purpose of an abstract class is to function as
               a base for subclasses. For example, there are situations in which you will want to define a
               superclass that declares the structure of a given abstraction without providing a complete
               implementation of every method. That is, sometimes you will want to create a method. That
               is, sometimes you will want to create a superclass that only defines a generalized form that
               will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a
               class determines the nature of the methods that the subclasses must implement.

               Interface in Java
                       An  interface  in  Java  is  a  blueprint  of  a  class.  It  has  static  constants  and  abstract
               methods.
                       The  interface  in  Java  is  a  mechanism  to  achieve  abstraction.  There  can  be  only
               abstract methods in the Java Interface, not method body. It is used to achieve abstraction and
               multiple inheritance in Java.
                       In other words, you can say that interfaces can have abstract methods and variables.
               It cannot have a method body.
                       Java Interface also represents the IS-A relationship.
                       It cannot be instantiated just like the abstract class.

               Why use Java Interface?
                       There are mainly three reasons to use interface. They are given below.
                       -   It is used to achieve abstraction.
                       -   By interface, we can support the functionality of multiple inheritance.
                       -   It can be used to achieve loose coupling.

               How to declare an interface?
                       An interface is declared by using the ‘interface’ keyword. It provides total abstraction;
               means all the methods in an interface are declared with the empty body, and all the fields are
               public, static and final by default. A class that implements an interface must implement all the
               methods declared in the interface.
               Syntax:

                        interface <interface_name> {

                               // declare constant fields
                               // declare methods that abstract by default
                        }






                                                                                                            60
   279   280   281   282   283   284   285   286   287   288   289