Override

차노·2023년 8월 3일
0

JavaScript is an interpreted language, child class method overrides the parent class's method.

How to override

While calling the "alert()" method, the overridden alert() will be called as shown in above output:

Reference

개념

In object-oriented programming(oop), 'override' refers to the practice of providing a new implementation for a method that is already defined in a superclass (parent class)/ within a subclass (child class).

부모 클래스에서 이미 선언된 메소드를 서브 클래스에서 처음 실행할 때의 행동을 나타낸다.

When a method in a subclass has the same name, return type, and parameters as a method in its superclass, it is said to override the superclass's method.

서브 클래스에서 메소드의 이름, 리턴 타입, 파라미터가 부모 클래스와 같을 때, 슈퍼 클래스의 메소드를 오버라이드 했다라고 한다. (상속의 의미)

Points

  1. Method signature: To override a method, the method in the subclass must have the same method signature (method name, return type, and parameter types) as the methods in the supclass.

    하위 클래스가 부모 클래스의 메소드를 오버라이드 하려면 같은 이름, 타입, 파라미터여야 한다.

  2. Inhertiance: Method overriding is closely related to inheritance. When a subclass inherits a method from its superclass, it can choose to provide its own implementation for that method. This allows the subclass to customize or extend the behavior of the intherited method.

  3. Access Control: The overriden method in the superclass must be accessible to the subclasses. In other words, it should not be a private method. Typically, overriden methods are declared as protected, public, or package-private (default visibility) in the superclass to allow subclasses to override them.

  4. @Override Annotation (Java): In some programming languages like Java, you can use the @override annotation before a method to explicitly indicate that you intend to override a method from the superclass. This annotation helps catch compliation errors if the method signature in the subclass doesn't match the superclass.

하위 클래스가 상위 클래스에 올라탄다라고 생각하면 된다.

0개의 댓글

관련 채용 정보