In: Computer Science
Java Problem:
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.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
/* INTERFACE: In interface all the methods are declared but not defined.
* All the methods are public in an interface.*/
// create interface 1
public interface Interface1
{
// just declare the methods don't define them
void method1();
void method2();
}
// create class by implementing new interface
class Class1 implements Interface1 {
// define the methods here along with their body.
@Override
public void method1() {
System.out.println("This is Method1");
}
@Override
public void method2() {
System.out.println("This is Method2");
}
// TEST the interface
public static void main(String[] args) {
// create object
Class1 class1 = new Class1();
// call methods
class1.method1();
class1.method2();
}
}
======

