Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 53

Thread: Interface?

  1. #21
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    interface Dog has myMethod();

    and class BigDog implements Dog

    inside BigDog i still have to type

    Public void myMethod(){

    }

    so what is the point of interfaces?
    Are you comparing class vs. interface? Are you asking why will you use an interface if you still have to type
    the method you have declared inside the interface? And why not use a class that has a method myMethod and the code is already there like:

    Java Code:
    //class Dog has method of bark();
     public class Dog
     {  ....
        void bark()
        {   //inside this method has already a code for a dog to bark
        }
        ...
     }
    
    
    and so at class BigDog you will just call bark() method at Dog class so that inside BigDog you dont have to type bark() method?
     public class BigDog
     {  ....
        void makeItBark()
        {   Dog.bark();
        }
        ...
     }
    COMPARE to an INTERFACE where you have to type and type the code everytime you implement Dog interface
    Java Code:
    //interface Dog has method of bark();
     interface Dog
     {  void bark();
     }
     
    //implement Dog at BigDog and type how dog will bark
     public class BigDog implements Dog
     {  ....
        void bark()
        {   //type code here
        }
        ...
     }
    
    //implement Dog at SmallDog and type how dog will bark
     public class SmallDog implements Dog
     {  ....
        void bark()
        {   //type code here AGAIN
        }
        ...
     }
    
    //implement Dog at SmallDog and type how dog will bark
     public class TypicalDog implements Dog
     {  ....
        void bark()
        {   //type code here AGAIN
        }
        ...
     }

  2. #22
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Wonder what mine is trying to say... Something tells me he doesn't understand interfaces.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #23
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by mine0926 View Post
    Are you comparing class vs. interface? Are you asking why will you use an interface if you still have to type
    the method you have declared inside the interface? And why not use a class that has a method myMethod and the code is already there like:

    Java Code:
    //class Dog has method of bark();
     public class Dog
     {  ....
        void bark()
        {   //inside this method has already a code for a dog to bark
        }
        ...
     }
    
    
    and so at class BigDog you will just call bark() method at Dog class so that inside BigDog you dont have to type bark() method?
     public class BigDog
     {  ....
        void makeItBark()
        {   Dog.bark();
        }
        ...
     }
    COMPARE to an INTERFACE where you have to type and type the code everytime you implement Dog interface
    Java Code:
    //interface Dog has method of bark();
     interface Dog
     {  void bark();
     }
     
    //implement Dog at BigDog and type how dog will bark
     public class BigDog implements Dog
     {  ....
        void bark()
        {   //type code here
        }
        ...
     }
    
    //implement Dog at SmallDog and type how dog will bark
     public class SmallDog implements Dog
     {  ....
        void bark()
        {   //type code here AGAIN
        }
        ...
     }
    
    //implement Dog at SmallDog and type how dog will bark
     public class TypicalDog implements Dog
     {  ....
        void bark()
        {   //type code here AGAIN
        }
        ...
     }
    this is exactly what i'm thinking.

    it seems easier to me to just define the methods inside the class. but i'm sure i'll figure it out..

    maybe the tutorials were just showing me How to setup a interface.. the structure of it, and later tutorials will show me the Why and When

    got a feeling i may be getting a bit ahead of myself now :confused:

  4. #24
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am trying to understand what OP do not understand in interface. Also, I am not sure if I understand interface.

    I saw this site http://www.iam.ubc.ca/guides/javatut...erfaceDef.html
    and it makes me apply interface ActionListener and method actionPerformed(ActionEvent ae). Where
    actionPerformed(ActionEvent ae) let us code the event we want, example of this is a JButton.

    I am developing an application that connects to a database and thinking if where can I apply interface.

    I am thinking if did I use class when I should use interface instead???

    For the beginners like me I would use class than using an interface. Just like on my last post methods inside
    a class has already a code than an interface where you just declare the method but have to write the code for
    that method again and again everytime I implement it.

    Hope you understand what I am trying to say, Some of my english is not good and I am having hard time to explain it in english
    Last edited by mine0926; 11-04-2010 at 06:25 AM.

  5. #25
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Maybe my question will help to answer the question. Cause i also dont fully understand interfaces.

    Are Abstract Class/Methods the same thing as Interface Class/Methods?

    I was first introduced to what Abstracts are but then just barely touched on interfaces (it seemed). I never understood the true difference between Abstract and Interface. Therefore, i only used Abstract.

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I'll try another angle.

    As said earier, an interface is a promise that the implementing class will be able to do something. It's a contract, if you like. So something implementing Something (below) says that it has a doSomething() method.
    Java Code:
    public interface Something {
    void doSomething();
    }
    Now, if I have a method which takes in a Something object then I know the compiler will prevent me from passing in something that isn't Something.
    Java Code:
    public void processSomething(Something s) {
        s.doSomething();
    }
    You asked why not simply add the method directly to the class? Well, if I have my class MyClass that has a method doSomething() I cannot pass that into the above method because it isn't a Something. I could change the above method to take a MyClass instead:
    Java Code:
    public void processSomething(MyClass mc) {
        mc.doSomething();
    }
    However, I can now only pass in objects that are of MyClass. This is a bit restrictive. To take the Comparable example then everty clas that wanted to be Comparable would have to extend Comparable...and since Java only allows one parent class...well, it would quickly become impossible, I mean, what if your class was not only Comparable, but also had to doSomething() (ie be Something)?

  7. #27
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    yep i get that part how you can pass it in but doSomething is empty ?? what does it do? y would you need to pass it in it's just been declared but nothing actually happens in it

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    The thing is doSomething() isn't empty. It is filled with whatever code is in the implementing class.
    Java Code:
    public class ImplementingSomething implements Something {
        public void doSomething() {
            System.out.println("done");
        }
    }
    or
    Java Code:
    public class AlsoImplementingSomething implements Something {
        public void doSomething() {
            System.out.println("also done");
        }
    }
    If I instantiate an object of ImplementsSomething I can pass it into that method. But I can also pass in an AlsoImplementsSomething:
    Java Code:
    ImplementsSomething is = new ImplementsSomething();
    AlsoImplementsSomething ais = new AlsoImplementsSomething();
    processSomething(is);
    processSomething(ais);
    Each of those calls to processSomething will call doSomething, which maps to the relevant implementation. So I can get it to do different things depending on the class.

  9. #29
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    See if i can get this..


    Something.java
    Java Code:
    public interface Something {
    void doSomething();
    }
    Implementing.java
    Java Code:
    public class ImplementingSomething implements Something {
        public void doSomething() {
            System.out.println("done");
        }
        
    }
    application.java
    Java Code:
    class Application{
    
      public static void main(String[] args){
          ImplementsSomething is = new ImplementsSomething();
          
          processSomething(is);
    
      }
       public void processSomething(Something s) {
              s.doSomething();
       }
    }
    Last edited by maknib; 11-04-2010 at 09:41 AM.

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Yes, except of course that is a very simple example and so the reason for interfaces really doesn't appear in it.

    The advantage is that you can pass anything into processSomething so long as it implements Something.

  11. #31
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Is useing an interface a way to remove abstract methods from an abstract class?

  12. #32
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    I;ve been reading the Java tutorials.

    i'm almost up to Interfaces, so i got a feeling i will soon be able to understand this concept. soon.

    maybe, it's for the programmers to know what they must use in it. so if 3 different programmers create an object that implements an interface they all know what they must use but in different ways. Maybe :P

    theres also the example that to start a car i know i must put my key in and turn it in the so called "interface"

    or to turn a television on i press the power button. i know i need to but don't need to know why.

    but we will see after i learn more. i think i might finally understand it soon yay!

  13. #33
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Ok, lately i've been looking around to better understand what interfaces are, because i have the same problem as you. I tried asking a simple question to see if i might be able to answer your question, but no one replied to my question.:mad:

    The question i gotta ask you is... have you reach a point where you inherit other files (parent/child files)? If not, then you got a lil more to go to fully understand the meaning of interfaces. I'll explian what i learned though.

    I first learned about abstract classes and then just barely touched on what interfaces are and what their purpose is. From an abstract point of view, i learned how to 'combine other files into one'; not link other files into one, but actually combine multiple files, which in turn. has one common file. The reason for this was to combine other common methods and functionalities into one file that uses the same basic/primitive code that they all have in common with.

    For example, imagine you had to make a simulation of a natural environment that consists of herbs, herbivores, omnivores, and carnivores. They all have a common trait, they all consume energy, move around, reproduce, and produce offspring. Some of these common functions already have code in it that they all have in common with, but sometimes each type would have a code that was unique to its class, different code from others. When that happens, i was taught to, use an abstract method (which seems very much like a method in the interface), which means, theres no code inside of it, but every other file that implements it has to have that method and call it (super()). Usually you use an abstract method if its a method that they all have, its just, the code is completely different from all the others.

    From a different point of view that i see. If you have a program that uses a another program (inherits it in-other-words) that has an interface, or an abstract method, then you have to make a method to least to use the other program. A website i found best explains it (1)"To them, your interface is only incidental, something that have to add on to the their code to be able to use your package". So what the other were saying, its a "promise", it was just a vague explanation (no offence, maybe i'm just as bad).
    Another example, if you make a program that uses another program, it might have an interface that requires a security method, graphics method, etc. So therefore, you must use that method (with abstract methods {that consists of multiple files to basically make one}, there might be code already in it) in order to even use the program that interacts with it (inheritance). From abstracts, you use the super() syntax.

    ...But my question to you still remain an important one. If you haven't began to understand inheritance (child/parent), or combining other files into one, then you got a lil more to go to grasp the full concept of it.



    (1) interface vs abstract class : Java Glossary
    (2) Abstract classes vs. interfaces - JavaWorld

    Anyways, I hope this might help, or at least look in a different point of view.
    Good luck and hope you have fun :)

  14. #34
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Hey Champ,

    Yeahi'm pretty sure i understand them

    but.. hmmm lets see if i can write something..


    Java Code:
    class Application{
      
      public static void main(String[] args){
        
        Dog myDog = new Dog(7);
        if(myDog.legs > 4){
          System.out.println("My Dog has " + myDog.legs +" legs!");
       }else{
          System.out.println("My Dog has " + myDog.legs +" legs! He is a Freak!");
      }
    
    
    }

    Java Code:
    class Dog extends animal(){
    
      public void Dog(int l){
          setLegs(l);
      }
    
    }
    Java Code:
    class Bird extends animal(){
    
      public void Bird(int l){
         setLegs(l);
      }
    
    }



    Java Code:
    class Animal{
      
      public Animal(){
         int legs;
      }
      
      private void setLegs(int nlegs){
        legs = nLegs;
      }
    }
    My Dog has 4 legs!

    of course my syntax woun't be correct :p
    Last edited by maknib; 11-05-2010 at 04:55 AM.

  15. #35
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    That syntax is way off :p Vaguely looks like Python, Java, and C++ squashed into one. In any case, I don't see why it would say your dog has 4 legs, even with proper syntax, if you create it with 7... And I don't see any interfaces, which are what we're talking about here

    Anyway, My opinion of the Sun/Oracle tutorials on interfaces is that they tell you how to use them, but not why. This thread may explain why that is, though - the why is damn hard to explain to someone who's never needed an interface.

    @Joker: Methods in interfaces are implicitly declared as 'public abstract' RETURNTYPE METHOD. So yes, they are the same as abstract methods, but abstract classes are limited by single inheritance.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  16. #36
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Singing Boyo View Post
    That syntax is way off :p Vaguely looks like Python, Java, and C++ squashed into one. In any case, I don't see why it would say your dog has 4 legs, even with proper syntax, if you create it with 7... And I don't see any interfaces, which are what we're talking about here

    Anyway, My opinion of the Sun/Oracle tutorials on interfaces is that they tell you how to use them, but not why. This thread may explain why that is, though - the why is damn hard to explain to someone who's never needed an interface.

    @Joker: Methods in interfaces are implicitly declared as 'public abstract' RETURNTYPE METHOD. So yes, they are the same as abstract methods, but abstract classes are limited by single inheritance.


    it is Maknib++

    it would print out 7 not 4.. i didn't have the if statement in there when i first made it :)

    i was showig this for Joker to show that i believe i understand Inheretence

    and i'm just going to finish the tutorials and hopefully by the time i Need to use Interfances i'll know why i need to use them

  17. #37
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Ah ok, i think i see the picture a lil bit better. So in other words, with an abstract class, you can only have one parent, but an interface, you can add multiple parents?

  18. #38
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    @ maknib's last post

    ah ok, nice :)
    just one thing that you might of forgotten or haven't touched on.

    here's a vague example
    Java Code:
    public class SpiderLegs
    {
    	public static void main(String[] args)
    	{
    		Spider8Legs spid8 = new Spider8Legs();
    		Spider6Legs spid6 = new Spider6Legs();
    	
    		//blah blah blah code
    	}
    }
    
    public class Spider8Legs extends Spider
    {
    	protected int _legs;
    	
    	public Spider8Legs()
    	{
    		super();
    		createLegs();
    	}
    	protected void createLegs()//this is required code
    	{
    		_legs = 8;
    	}
    }
    
    public class Spider6Legs extends Spider
    {
    	protected int _legs;
    	
    	public Spider6Legs()
    	{
    		super();
    		
    		createLegs();
    	}
    	protected void createLegs()//this is required code
    	{
    		_legs = 6;
    	}
    }
    
    //ABSTRACT FILE
    public abstract class Spider
    {
    	public spider()
    	{
    		//blah blah common code
    	}
    	protected abstract createLegs();
    	
    }


    it seems very similar to me. when i first read this thread, i thought introducing abstracts could help connect the dots a lil better for you :)
    Last edited by JoKeR313; 11-05-2010 at 06:44 AM.

  19. #39
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thanks for the patience explaining to us what is interface. I am understanding it based on your examples.
    I still have question, I understand Tolls' explanation (a little I think) but I am confuse between interface
    and class.

    What make you think or what is the circumstances that you need to use interface and not class?

    And I am thinking that the things that can be done in interface, a class can do it also.

    Here is an interface Interface_Something that does not have code at its method doSomething();
    Java Code:
    public interface Interface_Something
    {   public void doSomething();
    }
    then when you have to implement Interface_Something we will write something the method doSomething().
    Java Code:
    public class Implement_Interface_Something implements Interface_Something
    {   public void doSomething()
        {   System.out.println("Implement Interface_Something DONE");
        }
    }
    
    
    public class AlsoImplement_Interface_Something implements Interface_Something
    {   public void doSomething()
        {   System.out.println("ALSO Implement Interface_Something DONE");
        }
    }
    Here is what I did in a class that we were talking to, and maybe what makes me confuse too...
    Java Code:
    public class Class_Something
    {   public static void has_doSomething()
        {   System.out.println("JUST like Implement_Interface_Something code in doSomething() method \n"
                    + "----> Implement Interface_Something DONE");
        }
    }
    
    
    public class Class_Something_Also
    {   public static void has_doSomething_also()
        {   System.out.println("JUST like AlsoImplement_Interface_Something code in doSomething() method \n"
                    + "----> Implement Interface_Something DONE");
        }
    }
    Here is how I am thinking that class can do what interface can do.
    Java Code:
    public class Class_Main
    {   public Class_Main()
        {
            Implement_Interface_Something iis = new Implement_Interface_Something();
            AlsoImplement_Interface_Something aiis = new AlsoImplement_Interface_Something();
            processFor_InterfaceSometing(iis);
            processFor_InterfaceSometing(aiis);
    
            processFor_ClassSometing();
        }
    
        private void processFor_InterfaceSometing(Interface_Something is)
        {   is.doSomething();
        }
    
        private void processFor_ClassSometing()
        {   Class_Something.has_doSomething();
            Class_Something_Also.has_doSomething_also();
        }
    
        public static void main(String[] args)
        {   Class_Main cm = new Class_Main();
        }
    }
    implementing interfaces and coding doSomething() method everytime you implement it is
    just like creating different classes that has a doSomething method.

  20. #40
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    what about a fire truck? hmm??


    class Vehicle


    class truck extends vehicle
    class car extends vehicle
    class motorcycle extends vehicle
    class bus extends vehicle


    class emergencyVehicle extends Vehicle

    Class policeCar ???

    should this extend Car or should it extend emergencyVehicle?
    it should extend emergency vehicle because it needs the features of an emergencyvehicle.. but it should extend car because it is a car too but oh noes!! it Cant extend both can it..

    maybe i need an interface to declare all the common methods for policeCar, fireTuck, policeBike...


    then the police car can extend Car and implement EmergencyVehicle interface which declares the siren and things needed for an emergency vehicle?

    hmm
    Last edited by maknib; 11-05-2010 at 08:32 AM.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. interface
    By tatya in forum New To Java
    Replies: 1
    Last Post: 06-19-2010, 06:22 AM
  2. Is there a GUI interface for...
    By smith427 in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 07:17 AM

Posting Permissions

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