OOP Interfaces Demo in Java - Part 1
OOP Interfaces Demo in Java - Part 1
Introduction
The concept of an Interface in Java is designed to replace the functionality of multiple inheritance. C++, another object oriented programming language, supports multiple inheritance, Java does not. While Interfaces are very common in Java programming, some programmers avoid them. This example of OOP Interfaces Demo in Java provides an introduction to creating, implementing, and applying interfaces in the Java programming language. We illustrate OOP concepts with functional java source code and accompanying documentation.
This tutorial is published in 5 parts. This is the first part. At the bottom of this hub are links to the next 4 parts.
The first part of this tutorial contains a main() that serves as the entry point to our program. The main() is almost always a good place to begin; it's serves as an overview of the entire program.
package InterfaceDemo; /** * Main.java * Entry Point for the project. * This class provides a main() method that kicks off everything else. * This class can be replaced; the other classes will still be functional. * @author nicomp * */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // Instantiate objects of type Car and NetBeansProject // Note that the two type of objects have little in common, conceptually. Car myCar = new Car(); NetBeansProject myNetBeansProject = new NetBeansProject(); // This is an example of early binding myCar.Build(); myNetBeansProject.Build(); // Here's where it gets powerful BaseClass[] myObjectArray; // What is instantiated here? // The data type of myObjectArray is... // 1. 2 Base Class objects // or // 2. An array of 2 refrences (pointers) to base class objects // or // 3. Both // The answer is: #2. We create 2 references myObjectArray = new BaseClass[2]; int i; // Instantiate an object of type Car and store the reference in the array myObjectArray[0] = myCar; // Instantiate an object of a different type and store the reference in the SAME array myObjectArray[1] = myNetBeansProject; System.out.println("Entering loop to process base class objects..."); for (i = 0; i < myObjectArray.length; i++) { // At run-time the correct Build() method is called by the JVM // A good example of late binding myObjectArray[i].Build(); // A call-back. Sweet. } } }