[Java]Object oriented programming-OOP

William Parker·2022년 11월 7일
0
post-thumbnail

Object oriented programming

  • What is a class?
    • Class can be said to define the common properties of the object to be expressed in one place. In other words, a class defines the properties of an object.
    • Also, the information inside the class is called member variable
  • What is Instance?
    • An object created from a class is called an instance of that class.
class Phone {
    String model;
    String color;
    int price;
}

public class Main {
    public static void main(String[] args) {
        Phone galaxy = new Phone(); 
        galaxy.model = "Galaxy10";
        galaxy.color = "Black";
        galaxy.price = 100;
        
        Phone iphone =new Phone();
        iphone.model = "iPhoneX";
        iphone.color = "Black";
        iphone.price = 200;
        

        System.out.println("I bought " + galaxy.model + galaxy.color + " + this color " + galaxy.price + ".");
        System.out.println("My frined bought " + iphone.model + iphone.color + " + color" + iphone.price + ".");
    }
}

A class called Phone contains information about the model, color, and price of the computer. Using this, I created different instances of galaxy and iphone with the same properties such as model, color, and price.

  • method
    • let's talk about methods. You can think of a method as a bundle of code that performs a certain task.

    • Why do you need a method?

      1. Reusability
      • After creating a method, it can be reused repeatedly. Of course, it can be used in other programs as well.
      1. Remove duplicate code
      • When writing a program, the same code is often written over and over again. In this case, if you use a method, it will be more efficient code by eliminating the duplicate part.
      1. Program Structure
      • You can understand structuring by looking at the example below.
          ```
          int[] heights = new int[5]; // array of keys
          initHeight(heights); // 1. Initialize the key
          sortHeight(heights); // 2. Sort the keys in ascending order
          printHeight(heights); // 3. Print the sorted keys
          ```
  • As you can see, the code is classified and structured according to what kind of work it does. When writing extraordinarily long code, this approach makes it easier to modify and maintain.
  • When creating a method, if you name it well to express the contents of the method well, it is good to read the code at a glance without looking inside the method. This is expressed as good readability. For this basic quality of readability, there are two basic promises that must be kept when creating methods in Java.
    1. It must start with a verb.
    2. It should be written as camel case. (The first word is lowercase, and then words with only the first letter are capitalized depending on the division of words. There are no spaces or special characters in the middle.)
  • Method declaration and implementation
    • If you know the role of a method and why you need it, you should know how to implement it, right?
    • Method can be defined in the following format.

The method name is the name, and the code to be executed is the execution code, but what is the return type? The method returns the result of execution through the return statement. In this case, the part that determines the data type of the result is the return type.

Features of OOP

  1. Inheritance and Interfaces (Hierarchy)
  2. Polymorphism, ease of use (modularization)
  3. Encapsulation, information hiding
  4. Material Abstraction (Modeling)
  5. Dynamic Binding

  • Each box is an object, and in Java terms, it is a class.
  • Inheritance was created to express these hierarchies.
  • A child object inherits the characteristics of a parent object (parent).

For example, mammals, birds, and reptiles inherit the characteristic 'moving' of animals.
In other words. This means that the method move() can be used by mammals, birds, and reptiles.

As another example, if a variable is also declared in an object, it will be inherited.

If the age variable is declared in the animal object, it is inherited.

The form of inheritance differs depending on whether the method OR variable is 'implemented' or 'used' as it is.

  1. extends
    All declarations/definitions are done in parent, and children can use methods/variables as they are.
  2. implements (interface implementation)
    The parent object is only declared, and the definition (content) must be overridden (overridden) in the child.
  3. abstract
    A mix of extends and interface. extends, but some are implemented as abstract methods

Extends

In fact, extends is a representative form of inheritance.
You can use the parent's method as it is, and you can directly use the one implemented in the parent without overriding it.

 class Vehicle {
  protected int speed = 3;
  
  public int getSpeed(){
    return speed;
  }
  public void setSpeed(int speed){
    this.speed = speed;
  }
}

class Car extends Vehicle{
  public void printspd(){
    System.out.println(speed);
  }
}

public class ExtendsSample {
  public static main (String[] args){
    Car A = new Car();
    System.out.println(A.getSpeed());
    A.printspd();
  }
}

A class called Car inherits from Vehicle. And I printed speed with getSpeed() method.
However, I used speed directly in the vehicle's getSpeed ​​method. Extends means that not only methods but also variables can be used.

Multiple inheritance means that more than one parent class can exist, but Java does not support this. That is, public class Son extends Father, Mother{...} This is not supported.

implements

interface TestInterface{
  public static int num = 8;
  public void fun1();
  public void fun2();
}

class InterfaceExam implements TestInterface{
  @Override
  public void fun1(){
    System.out.println(num);
  }
  
  @Override
  public void fun2() {
    
  }
}

public class InterfaceSample{
  public static void main(String args[]){
    InterfaceExam exam = new InterfaceExam();
    exam.fun1();
  }
}

The biggest feature of implements is that the parent method must be overridden (overridden) like this.

Also, this implements replaces multiple inheritance.

Brief Summary

  1. extends is used for general class and abstract class inheritance, and implement is used for interface inheritance.
  2. When a class inherits from a class, we use extends, and when an interface inherits from an interface, we use extends.
  3. When a class uses an interface, it must use implements.
  4. You cannot use implements when an interface uses a class.
  5. extends can inherit only one class.
  6. extends The class itself uses the functions of the parent class.
  7. Multiple implements can be used.
  8. implements can be implemented for design purposes.
  9. The implemented class must use all the contents of implements.

extends extends a class and implements implements an interface.
The difference between an interface and an ordinary class is that the interface does not need to implement the defined method.
You just need to implement the method defined in the interface in the class that inherits the interface.

Reference

  1. https://velog.io/@hkoo9329/%EC%9E%90%EB%B0%94-extends-implements-%EC%B0%A8%EC%9D%B4

  2. https://zbomoon.tistory.com/13

profile
Developer who does not give up and keeps on going.

0개의 댓글