Computer Science, asked by ritesh7229983, 3 months ago

Q4) Declare Prime as an interface having prime() as method in one package and declare Odd as
another interface having odd() as method in same package. Define a class named Check in
another package that implements both interfaces. Define another class named Design that
extends the Check class to display the result.​

Answers

Answered by 20144415
0

Answer:

Introduction

There is no concept of multiple-inheritance in Java, but, Interfaces in Java are, for the most part, unique to the language, play a role similar to that of multiple-inheritance. Another unique feature in Java is Packages. Packages are containers for classes that are used to keep the class name space compartmentalized. By organizing the users classes into packages, their reusing to write other classes are easier. This Chapter is for getting a lesson of Interfaces and Packages.

Interfaces in Java

An interface in Java is essentially a special kind of class. Like classes, interfaces contain methods and variables; unlike classes, interfaces are always completely abstract. Let us first take a look at the general form of an interface definition.

Thus, an interface is defined just like a class except the key word interface in place of class. Although member elements are present in an interface they are static and final; although methods are defined in an interface, the code to implement the method is not. While classes can inherit from only one super class but, interfaces are able to inherit from as many as interface one can provide.

After defining an interface, it is require to implement with classes which can be done with the following syntax :

Now, let us consider an example to illustrate the use of interface technique. See the Figure 4.1 Here, Teacher and Student are two classes each of them having their own unique components. We want to define an interface, say, Resume which will include all the components both in Teacher and Student.

in this example, the interface Resume is declared which is implemented by two classes: Teacher and Students; Teacher is an inherited class from a class Employee ( the definition of all classes are understood ). With this definitions it should be noted that the method (s ) which is / are in interface must be defined in their respective classes with which the interface is implemented. We can, therefore, run the following through a main class which extends the above defined interface, namely Resume.

Note that for object.bioData( ); at line 5, and object.bioData ( ); at line 7 of the above piece of codes, the respective codes for preparing bio-data of Teacher and Student will be in effect.

If the above mentioned example is a skeleton of how a method is sharable in two classes then following is the Illustration 4.2 giving executable code how member elements are sharable.

Explanation:

hope it helps you

Similar questions