简体   繁体   中英

what is the difference between implementing from an interface and extending from the object class?

I'm new to java, I hope this question isn't too basic. I'm having trouble understanding why interfaces are needed, and what the difference is between implementing and inheriting.

If I would want to use the Comparable method in my program, (java.lang.Comparable) why can't I just make my class inherit from Comparable ?

public class Main {

    public static void main(String[] args) {

    class DocumentReader extends Comparable;

Why does IntelliJ say 'no interface expected here'?

Loosely speaking, and not entirely accurate since JDK 8... but ok for grasping the basics.

An interface is a specification of methods that a class implementing the interface must have. The interface provides no implementation. Thus, saying your class 'implements Comparable' places an obligation on it to provide a compareTo method with a particular signature.

A class may contain actual method implementations, and perhaps member data. Extending a class is essentially a specialization of that class. You get everything the base class does, which you can perhaps modify (by overriding base methods) or augment (by adding new methods).


EXCEPT that there are such things as abstract methods, where the base class provides no implementation, and thus forces the extending class to provide an implementation.

From one point of view, then, you could regard an interface as an abstract class in which all methods are abstract. This would be workable, except that Java only allows a class to extend one class (no 'class X extends A, B'), but a class can implement more than one interface ('class X implements A, B' - my syntax might not be exact). There's a technical reason for the restriction on inheriting from multiple classes, but I'll skip that here.

As I remarked at the start, this is not entirely true as of JDK 8 - there are 'default methods' in interfaces, so the line gets a little blurry.

I'd regard an interface as like a specification for the kind of plugs and sockets an object must provide if it's to be connected to other objects in standard ways. You want your objects to be 'compared' so they can be arranged in order by other components? Then you must implement Comparable.

i am trying to explain programmatically. Extends : When you want to extend a subclass to be extended in inheritance that we use Java extends. Implements: When an implement an interface, we use the keyword implement.

Extend vs Implements : In short, extends is for extending a class and implements are for implementing an interface.

As Java doesn't support multiple inheritances and this problem overcomes by multiple interfaces.

Extends:

class Person
  {
     String name;
     Person(String n)
      {
        name = "Person:  " + n;
      }
   }
class Mother extends Person
  {
     Mother(String n)
     {
       super(n);
       name = "Mother:  " + n;
     }
  void FeedChildren()
    {
       System.out.println(name + " is feeding the kids ...");
    }
 }
class Wife extends Person
  {
    Wife(String n)
     {
         super(n);
         name = "Wife:  " + n;
      }
  void CallHusband()
   {
      System.out.println(name + " is calling the husband ...");
   }
}
public class Test
  {
      public static void main(String args[])
        {
            Person p = new Person("Prerna");
            Mother m = new Mother("Mahima");
            Wife w = new Wife("Raani");
            System.out.println("p is a " + p.name);
            System.out.println("m is a " + m.name);
            System.out.println("w is a " + w.name);
            m.FeedChildren();
            w.CallHusband();
        }
  }

Here the base class is Person, Mother, and Wife but when Wife and Mother are same the same person and that is the person we are talking about.

Implements

class Person
  {
    String name;
    Person(String n)
    {
      name = "Person:  " + n;
    }
 }
interface Mother
  {
    public void FeedChildren();
  }
interface Wife
  {
    public void CallHusband();
  }
class WifeAndMother extends Person implements Wife, Mother
  {
    WifeAndMother(String n)
     {
        super(n);
        name = "Wife and mother:  " + n;
     }
public void FeedChildren()
   {
       System.out.println(name + " is feeding the children.");
   }
public void CallHusband()
  {
      System.out.println(name + " is calling her husband.");
  }
}
public class Test
 {
    public static void main(String args[])
      {
          Person p = new Person("Prerna");
          WifeAndMother w = new WifeAndMother("Raani");
          System.out.println("p is a " + p.name);
          System.out.println("w is a " + w.name);
          w.FeedChildren();
          w.CallHusband();
      }
  }

您只能从一个类扩展并实现多个接口。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM