본문 바로가기

백엔드/JAVA

오버라이딩

클래스 

내가작성한내용

package intermediate;

public class InheritanceDemo {
  public static void main(String[] args) {
   Tiger tiger = new Tiger();
       tiger.eat();
       tiger.run();

       Eagle eagle = new Eagle();
           eagle.eat();
       eagle.fly();

       Goldfish goldfish = new Goldfish();
       goldfish.eat();
       goldfish.swimming();

  }


  class Tiger{
    String eye;
    String mouth;
    String legs;
    void eat (){}
    void sleep(){}
    void run(){
      System.out.println("달리다");
    }

  }
  class Eagle{
    String eye;
    String mouth;
    String wings;
    void eat (){}
    void sleep(){}
    void fly(){
      System.out.println("날다");

    }
  }
  class Goldfish{
    String eye;
    String mouth;
    String pins;
    void eat (){}
    void sleep(){}
    void swimming(){
      System.out.println("수영하다");

    }
  }
  }


 

 

 

ctrl + o  오버라이드 찾기

오버라이드 했을때는 데이터타입과 리턴타입이 똑같아아야 함

 

 public static void main(String[] args) {
    Child child = new Child();
    child.method3();
    child.method1();
    child.method2();


  }
}
class Parent {
  void method1(){
    System.out.println("method1");
  }
  void method2(){
    System.out.println("method2==parent");
  }
}

class Child extends Parent {
  @Override
  void method2() {
    System.out.println("method2= modfied by child");
    super.method2();
  }

  void method3(){
    System.out.println("method3 == added by child");

  }

 

 

 

import java.util.Calendar;  // 캘린더 추가

import static java.util.Calendar.getInstance;

public class StaticImportDemo {
  public static void main(String[] args) {
  getInstance(); 
  }
  

/*
  public class StaticImportDemo {
  public static void main(String[] args) {
  Calendar.getInstance(); // alt + Enter add  
  }
}

*/

 

 

 

alt insert

 

 //사각형 넓이구하기 
 
 public static void main(String[] args) {

    RactAngle ractAngle = new RactAngle(5,5);
    ractAngle.getArea();

    double area = ractAngle.getArea();
    System.out.println(area);
  }
}
  class Figure{
 
    private int poly;
    public double getArea(){
      return 0.0;
    }
  }
  class Triangle extends Figure {
    private int poly = 3;    //은닉
    private int height; // 은닉2
    private int width;  //만들어질때 초기화가 됨


    public Triangle(int height, int width) {
      this.height = height;
      this.width = width;
    }
    @Override
    public double getArea() {
      return (width * height) /(double) 2;
    }
  }

  public RactAngle(int height, int width) {
    this.height = height;
    this.width = width;
  }
  public double getArea(){
    return (width * height);
  }
}

 

반응형

'백엔드 > JAVA' 카테고리의 다른 글

java - lambda  (0) 2024.08.28
JAVAerror로그  (0) 2024.08.26
문자열 테스트  (0) 2024.08.21
열거형  (0) 2024.08.21
8/21 ch_05 배열,열거형  (0) 2024.08.21