Topic
Threads
Posts
Last Post
Interfaces
Locked after Sunday, February 3, 2019 11:30 PM EST.
1. Explain Java interface and its use in a few
sentences.
2. Write the code of one Java interface with one method.
3. Also write two Java classes that will implement the method of
this interface, but each class will implement this method from the
interface differently. Simply put System.out.println inside each
method with different output to demonstrate different
implementation.
4. Write a few sentences explaining your code and the expected
result/output.
1. An Interface in Java is just like a class except, it only have static constants and abstract methods. Interface is a way to achieve total abstraction in Java. It is also known as the blueprint of a class. Since we cannot achieve multiple inheritance in Java using classes, interfaces are used to achieve multiple inheritance. All the methods of an interface are implicitly public and abstract.
To implement an interface in our class we use the term "implements" between class_name and interface_name.
2. *********Java Code for Interface with one method***********
interface Pet{
public void pet_sound();
}
*********************************
3. *************Code for 2 classes implementing same method from interface Pet****************
interface Pet{ //defining interface
public void pet_sound(); //abstract method
}
class Dog implements Pet { //using Pet interface
public void pet_sound(){
System.out.println("The dog Barks: bow bow");
}
}
class Cat implements Pet { //using Pet interface
public void pet_sound(){
System.out.println("The cat says: mews mews");
}
}
//to demonstrate the different implementations
class MainClass {
public static void main(String[] args) {
Dog mydog = new Dog(); // Create a Dog
object
Cat mycat = new Cat(); // Create a Cat object
mydog.pet_sound();
mycat.pet_sound();
}
}
*******************************************************************
4. The Code is explained using the //comments.
The demonstrated output of the code is shown below:
Topic Threads Posts Last Post Interfaces Locked after Sunday, February 3, 2019 11:30 PM EST. 1....
Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...
Homework 3: Input Validation 1 Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2 User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...
Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in interesting ways. Now you’ll get a chance to use more of what objects offer, implementing inheritance and polymorphism and seeing them in action. You’ll also get a chance to create and use abstract classes (and, perhaps, methods). After this project, you will have gotten a good survey of object-oriented programming and its potential. This project won’t have a complete UI but will have a...