Java 객체지향의 특징 4가지

Ryan mingun choi·2023년 10월 19일
0
post-thumbnail

1.Encapsulation(캡슐화)
@개념: 각 요소들이 여러 가지 자료형으로 표현될 수 있다는 것

@예시코드:

    class Area {

      // fields to calculate area
      int length;
      int breadth;

      // constructor to initialize values
      Area(int length, int breadth) {
        this.length = length;
        this.breadth = breadth;
      }

      // method to calculate area
      public void getArea() {
        int area = length * breadth;
        System.out.println("Area: " + area);
      }
    }

    class Main {
      public static void main(String[] args) {

        // create object of Area
        // pass value of length and breadth
        Area rectangle = new Area(5, 6);
        rectangle.getArea();
      }
    }
  1. Inheritance(상속)
    @개념: 변수나 메소드를 직접 만들지 않고 부모클래스에서 상속을 받음으로써 자식클래스가 재사용할 수 있는 개념

    @예시코드:

    class Animal {

      // field and method of the parent class
      String name;
      public void eat() {
        System.out.println("I can eat");
      }
    }

    // inherit from Animal
    class Dog extends Animal {

      // new method in subclass
      public void display() {
        System.out.println("My name is " + name);
      }
    }

    class Main {
      public static void main(String[] args) {

        // create an object of the subclass
        Dog labrador = new Dog();

        // access field of superclass
        labrador.name = "Rohu";
        labrador.display();

        // call method of superclass
        // using object of subclass
        labrador.eat();

      }
    }
  1. Abstraction(추상화)
    @개념: 공통적이고 중요한 부분만 추려내어 일반화하는 것

    @예시코드:

      //abstract class 
      abstract class Bank{    
          abstract int getInterestRate();    
      }    
      //concrete class
      class Citi extends Bank{    
          int getInterestRate(){return 7;}    
      }
      //concrete class
      class HSBC extends Bank{    
          int getInterestRate(){return 6;}    
      }    

      class Main{    
          public static void main(String args[]){    
              Bank b;  
              b = new Citi ();      // concrete class object
              System.out.println("Citi Rate of Interest is: "+b.getInterestRate()+"%");    
              b = new HSBC ();        // concrete class object
              System.out.println("HSBC Rate of Interest is: "+b.getInterestRate()+"%");    
          }
      } 
  1. Polymorphism(다형성)
    @개념: 하나의 이름을 가진 클래스나 메서드가 여러 가지 형태의 동작을 하

    @예시코드:

        // Animal 
         public class Animal{
           public void sound(){
              System.out.println("Animal is making a sound");   
           }
        }

        //Horse
        class Horse extends Animal{
          @Override
          public void sound(){
              System.out.println("Neigh");
          }
          public static void main(String args[]){
              Animal obj = new Horse();
              obj.sound();
          }
        }
        
        //Cat
        public class Cat extends Animal{
          @Override
          public void sound(){
              System.out.println("Meow");
          }
          public static void main(String args[]){
              Animal obj = new Cat();
              obj.sound();
          }
		}
        
profile
finding happiness

0개의 댓글