Page 1 :
Friday, July 30, 2021, 8:09 PM, , Interface :, An Interface in Java programming is defined as an abstract type used to specify the behavior, of a class. A Java interface contains static constants and abstract methods. A class can, implement multiple interfaces. In Java, interfaces are declared using the interface keyword., All methods in the interface are implicitly public and abstract., , Syntax for Declaring Interface, interface name {, //methods, }, To use an interface in your class, append the keyword "implements" after your class name followed by, the interface name., Example for Implementing Interface, class Dog implements Pet, The rulebook for interface says,, ○ An interface is 100% abstract class and has only abstract methods., ○ Class can implement any number of interfaces., Example:, , interface Pet, {, public void test();, , COMBINED ISC 135 Page 1
Page 2 :
}, class Dog implements Pet, {, public void test( ), {, System.out.println("Interface Method Implemented");, }, public static void main( ), {, Dog p = new Dog();, p.test();, }, }, Difference between Class and Interface, Class, Interface, In class, you can instantiate variable and create an In an interface, you can't instantiate variable and, object., create an object., Class can contain concrete(with implementation), The interface cannot contain concrete(with, methods, implementation) methods, The access specifiers used with classes are private, In Interface only one specifier is used- public., protected and public., Must know facts about Interface, , ○ A Java class can implement multiple Java Interfaces. It is necessary that the class must, implement(define) all the methods declared in the interfaces., ○ Class should override all the abstract methods declared in the interface, ○ Class needs to provide functionality for the methods declared in the interface., ○ All methods in an interface are implicitly public and abstract, ○ An interface can extend from one or many interfaces. Class can extend only one class but, implement any number of interfaces, ○ The class cannot implement two interfaces in java that have methods with same name but, different return type., , An interface Data is defined with a data member and a, method volume() which returns the volume of the, implementing shape., A super class Base has been defined to contain the radius, of a geometrical shape., Define a sub-class CalVol which uses the properties of the, interface Data and the class Base and calculates the volume, of a cylinder., The details of the members of the interface and both the, classes are given below:, Interface name, , : Data, , COMBINED ISC 135 Page 2
Page 3 :
Data member:, double pi, : initialize pi = 3.142, Member function/method:, double volume(), , Class name, : Base, Data member/instance variable:, rad, : to store the radius in decimal., Member functions/methods:, Base(…), : parameterized constructor to initialize, the data members., void show(), : displays the radius with an appropriate, message., Class name, : CalVol, Data member/instance variable:, ht, : to store the height in decimal., Member functions/methods:, CalVol(…), : parameterized constructor to initialize, the data members of both the classes., double volume(), : calculates the volume of a sphere by, using the formula (pi × radius2 × height), void show(), : displays the data members of both, the classes and the volume of the, sphere with appropriate message., Assume that the interface Data and the super class Base, has been defined. Using the concept of inheritance, specify, the class CalVol giving details of the constructor, double, volume() and void show()., The interface, super class, main() function and algorithm, need not be written., , COMBINED ISC 135 Page 3