Question

Topic Threads Posts Last Post Interfaces  Locked after Sunday, February 3, 2019 11:30 PM EST. 1....

Topic

Threads

Posts

Last Post

Interfaces

pixel.gif

 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

1 interface Pet 3 public void pet_sound (O; 4. 7 Class Dog implements Pet t altaf@caesar: /Documents/Java 8 9 public void pet_sound (O 10 File Edit View Search Terminal Help altaf@caesar/Documents/Javaş javac new.java 11 System.out.println(The dog Barks: bowaltaf@caesar:/Documents/Javas java HelloClass 12 Hello altaf@caesar/Documents/Javaş javac new.java altaf@caesar:/Documents/Javaş java Mainclass The dog Barks: bow bow The cat says: mews mews altaf@caesar:-/Documents/3avas 14 16 17 class Cat implements Pet 18 19 public void pet_sound() 20 21 System.out.println(The cat says: mews 23 24 25 26 27 //to demonstrate the different impleme 28 29 class Mainclass 30 public static void main(String[] arg 31 32 34 35 36 37 Dog mydog -new Dog) I/ Create Cat mycat new Cat); // Create mydog.pet_sound (); mycat.pet sound ()

Add a comment
Know the answer?
Add Answer to:
Topic Threads Posts Last Post Interfaces  Locked after Sunday, February 3, 2019 11:30 PM EST. 1....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    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...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT