Results 1 to 3 of 3
  1. #1
    akhmad is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default how to call or use methods inside interface?

    Hi there, I am newly joined to this forum and this is my first post.
    I wanna help using interfaces.
    here are 3 java files,

    // This is interface:
    public interface FerrariInterface {

    void PrintCarName ( );
    void PrintOwnerName ( );

    }

    // This is a class which implements interface:
    public class CarDetails implements FerrariInterface {


    public void PrintCarName ( ) {
    System.out.println ("My name is FERRARI!");

    }
    public void PrintOwnerName ( ) {
    System.out.println ("I am the owner of Ferrari!");

    }

    }

    //This class calls class CarDetails's methods:
    public class MyOwnCar {

    public static void main (String[] args) {

    CarDetails TestObject = new CarDetails ( );
    TestObject.PrintCarName ( );
    TestObject.PrintOwnerName ( );

    }
    }

    My question is that in the main method I want to call interface FerrariInterface instead of class CarDetails?
    For that what I have to write inside the class MyOwnCar?
    I want to call PrintCarName ( ) and PrintOwnerName ( ) methods via interface FerrariInterface,
    is it possible, maybe I have to implement or extend FerrariInterface to MyOwnCar class.

    Please help me
    Thanks indeed
    Akhmad

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by akhmad View Post
    Hi there, I am newly joined to this forum and this is my first post.
    I wanna help using interfaces.
    here are 3 java files,

    // This is interface:
    public interface FerrariInterface {

    void PrintCarName ( );
    void PrintOwnerName ( );

    }

    // This is a class which implements interface:
    public class CarDetails implements FerrariInterface {


    public void PrintCarName ( ) {
    System.out.println ("My name is FERRARI!");

    }
    public void PrintOwnerName ( ) {
    System.out.println ("I am the owner of Ferrari!");

    }

    }

    //This class calls class CarDetails's methods:
    public class MyOwnCar {

    public static void main (String[] args) {

    CarDetails TestObject = new CarDetails ( );
    TestObject.PrintCarName ( );
    TestObject.PrintOwnerName ( );

    }
    }

    My question is that in the main method I want to call interface FerrariInterface instead of class CarDetails?
    For that what I have to write inside the class MyOwnCar?
    I want to call PrintCarName ( ) and PrintOwnerName ( ) methods via interface FerrariInterface,
    is it possible, maybe I have to implement or extend FerrariInterface to MyOwnCar class.

    Please help me
    Thanks indeed
    Akhmad
    Your object TestObject is of type CarDetails (obvously) but, because that class implements the interface FerrariInterface it is also of type FerrariInterface. In your main( ... ) method you could have written instead:

    Java Code:
    FerrariInterface TestObject = new CarDetails ( );
    TestObject.PrintCarName ( );
    TestObject.PrintOwnerName ( );
    Now the code is calling the methods declared in the interface and the result will be the same. There is no need to implement that interface in your MyOwnCar class (it only tests the other classes).

    kind regards,

    Jos

  3. #3
    akhmad is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default thats cool

    Quote Originally Posted by JosAH View Post
    Your object TestObject is of type CarDetails (obvously) but, because that class implements the interface FerrariInterface it is also of type FerrariInterface. In your main( ... ) method you could have written instead:

    Java Code:
    FerrariInterface TestObject = new CarDetails ( );
    TestObject.PrintCarName ( );
    TestObject.PrintOwnerName ( );
    Now the code is calling the methods declared in the interface and the result will be the same. There is no need to implement that interface in your MyOwnCar class (it only tests the other classes).

    kind regards,

    Jos
    Hi Jos, thats cool mate,
    I understand it, thanks mate
    regards
    Akhmad

Similar Threads

  1. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 12:47 PM
  2. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 06:08 AM
  3. Class inside an Interface
    By $hr!k@nt in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 10:56 AM
  4. Why methods in an interface cannot be static?
    By cbalu in forum Advanced Java
    Replies: 2
    Last Post: 12-12-2007, 07:57 PM
  5. How to call JSP inside Servlet
    By Swamipsn in forum Advanced Java
    Replies: 0
    Last Post: 07-31-2007, 12:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •