Extends vs Implements [java]

Kyle_Kim·2023년 8월 25일
0

extends는 상속받은 클래스의 fields나 methods들을 사용할 수 있다. extends를 사용하는건 extends functionality로 해석할 수 있다.

- 오직 한 클래스만 상속받을 수 있다(다중상속이 불가능하다)

class One {
    public void methodOne()
    {
  
        // Some Functionality
    }
}
  
class Two extends One {
  
    public static void main(String args[])
    {
        Two t = new Two();
  
        // Calls the method one
        // of the above class
        t.methodOne();
    }
}

implements는 상속받은 methods들을구현해서 사용해야 하기 때문에 implements라고 한다..

- extends는 하나의 클래스만 가능하지만 implements는 상속 받는데 제한이 없다.

// Defining an interface
interface One {
	public void methodOne();
}

// Defining the second interface
interface Two {
	public void methodTwo();
}

// Implementing the two interfaces
class Three implements One, Two {
	public void methodOne()
	{

		// Implementation of the method
	}

	public void methodTwo()
	{

		// Implementation of the method
	}
}
// Defining the interface
interface One {
  
    // Abstract method
    void methodOne();
}
  
// Defining a class
class Two {
  
    // Defining a method
    public void methodTwo()
    {
    }
}
  
// Class which extends the class Two
// and implements the interface One
class Three extends Two implements One {
  
    public void methodOne()
    {
  
        // Implementation of the method
    }
}

Interface는 다중 extends가 가능하다.

// Defining the interface One
interface One {
	void methodOne();
}

// Defining the interface Two
interface Two {
	void methodTwo();
}

// Interface extending both the
// defined interfaces
interface Three extends One, Two {
}
profile
Make Things Right

0개의 댓글