Question

In: Computer Science

Abstraction is the basis for good Object-Oriented design that is modular, reusable, and maintainable. Interfaces and...

Abstraction is the basis for good Object-Oriented design that is modular, reusable, and maintainable.

Interfaces and Abstract classes are two mechanisms in Java that provide high-level abstractions. An interface or abstract class is something which is not concrete, something which is incomplete.

For this discussion, what do you think is meant by “Programming to an interface”? What are some differences between Abstract Classes and Interfaces? Provide a simple example application with an interface definition and a class which implements the interface.

Solutions

Expert Solution

Interface : Interface in object oriented programming is done to achieve abstraction for the highly changeable program or code . It is basically a blur print or layout for the class. It consist of only static variables and abstract method which has no body.

Below is an example of an interface and how is it implemented in a class:

interface display{  

void show();  

}  

class hello implements display

{  

public void show()

{

System.out.println("Hello everyone");

}  

public static void main(String args []){  

hello obj = new hello ();  

obj. show ();  

}  

}

In the above code an interface display is defined which has a method show() with no body defined . Class hello implements the interface display and then it can use its method and define its body accordingly. When an object of that class is created and is used to call the show() method then the function that is defined in the class hello is performed on calling the method show(). This way method show() of interface can be used by multiple classes with different functioning. It gives flexibility to use the code multiple times which is written once and you don’t have to write it again and again. It is also easy to maintain the codes.

Meaning of Programming to an interface”:

By the above statement “Programming to an interface” it is meant that framing the classes based on the interfaces which defines the behavior or functioning or attributes of the class. In this process first the interface is created with its static variables and abstract methods and then the real classes are created implementing the interface.

Basically when there is a need for multiple implementations then programing to interface is done . It is a concept and not a keyword that is used for implementing interfaces. This design principal basically refers to the loose coupling of the different modules of an application . It is used when there is a dependencies among the modules . In such a scenario using interfaces helps to make changes to a particular module without changing all the other modules .

Difference between Abstract class and Interface :

Abstract class:

  1. It can have both abstract methods also and methods with the functioning defined i.e. body of the method.
  2. It is implemented using the keyword extends.
  3. A class can only extend one abstract class.
  4. They can have all kind of variables like static , final , non-final , non-static.
  5. It has members that are defined as private , protected .

public abstract class Vehicle

{


public abstract void speed();

public abstract void color()

{

System.out.println(“Color of the vehicle is white”);

}

}

Interface:

  1. Interfaces have methods that are abstract in nature and have no body or functioning defined . They are not implemented in interfaces.
  2. It is implemented using the keyword implements.
  3. A class can implement multiple interfaces .
  4. They can only define static and final variables.
  5. It has members that are defined as public by default.

public interface Vehicle

{
void speed ();

void color ();
}

Example of interface implementation :

import java.io.*;

interface shape

{

    void show();

}

class square implements shape

{

    int side = 4;

    public void show()

    {

        System.out.println("The shape is square with number of sides " + side);

    }

}

class Main

{

public static void main (String[] args)

    {

        square obj = new square ();

        obj.show();

   }

}

Screenshot :


Related Solutions

modular design for object oriented data and process modeling...identify situations that are better aligned to one...
modular design for object oriented data and process modeling...identify situations that are better aligned to one method or the other and justify your recommendation.
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes....
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes. How would the same concepts apply to data structures and how we tend to define and think of ADTs?
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes....
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes. How would the same concepts apply to data structures and how we tend to define and think of ADTs?
i need a full explanation in details Object Oriented Programming Principles Principles Polymorphism Inheritence Encapsulation Abstraction...
i need a full explanation in details Object Oriented Programming Principles Principles Polymorphism Inheritence Encapsulation Abstraction Class Object Method Message Passing
Explain Basic Characteristics of Object Oriented System Analysis & Design.
Explain Basic Characteristics of Object Oriented System Analysis & Design.
What are design patterns in object oriented programming? Minimum 200 words
What are design patterns in object oriented programming? Minimum 200 words
What factors should be considered in selecting a design strategy? (object oriented design and analysis) Name...
What factors should be considered in selecting a design strategy? (object oriented design and analysis) Name different computing Client-Server architectures and give examples of the architectures. What are the main principles for User Interface Design? Describe any one of the principles in detail. Name five steps in the user interface design process. Describe types of navigation control in the user interface. Name basic principles of output design.
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
what is the essence of the object oriented analysis and design approach with UML diagramming tools
what is the essence of the object oriented analysis and design approach with UML diagramming tools
What are the object oriented concepts and which all object oriented concepts are used in the...
What are the object oriented concepts and which all object oriented concepts are used in the given program? Consider the following code and explain how each of the object oriented concepts are applied in the given program. (CO1) class Vehicle { string brand; public: void honk(); void honk(int); }; void Vehicle::honk() { cout << "Tuut, tuut! \n" ; } void Vehicle::honk(int x) { for(int i=0;i<x;i++) cout << "Tuut, tuut! \n" ; } int main() { Vehicle V1; V1.honk(); V1.honk(3); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT