DEV Community

ivinieon
ivinieon

Posted on

Basic coding words

Basic coding words

1. What is an Object?
→ An entity that has states and behaviors.

Example of an Object:

//python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
person1 = Person("John", 30)

Enter fullscreen mode Exit fullscreen mode

In this code, Person is a class that represents a person. An object person1 is created from the class with the name "John" and age 30.

2. What is a Class?
→ A blueprint that describes the attributes and behavior of an entity. It is not an object, but objects are created from it.

  • instantiate : creating an object using the "new" keyword

Example of a Class:

class Car {
    private String name;
    private double speed;
    private Size size;

    public void start() { ... }
    public void stop() { ... }
}

Enter fullscreen mode Exit fullscreen mode

This class represents a car and has three attributes: name, speed, and size. It also has two methods: start() and stop(), which represent the behaviors of a car.

Next, we create three Car objects using the following code:

Car myCar = new Car("niro");
Car yourCar = new Car("sonata");
Car ourCar = new Car("spotage");

Enter fullscreen mode Exit fullscreen mode

These lines of code create three Car objects named myCar, yourCar, and ourCar, respectively. We use the new keyword to instantiate these objects, and we pass in a string argument to set the name attribute of each object.

3. What is a Variable?
→ A named container that holds a value. The value can be changed.

Example of a Variable:

int age = 36;
Boolean success = true;
Car myCar = new Car("niro");
String channelName = "easyCode";

Enter fullscreen mode Exit fullscreen mode

We define four variables named age, success, myCar, and channelName. The first two variables are of the primitive types int and Boolean, respectively. The third variable is of the Car class type, and we instantiate a new Car object and assign it to this variable. The fourth variable is of the String class type.

4. What is a Function?
→ A set of independent code that performs a task. It can be called by its name, and may or not may receive parameters or return values. It is resuable.

Example of a Function:

age = 37;
myCar = new Car("bmw");

Enter fullscreen mode Exit fullscreen mode

We change the value of the age variable from 36 to 37. We also create a new Car object with the name "bmw" and assign it to the myCar variable, effectively replacing the previous Car object that was assigned to it.

5. What is a Method?
→ A set of code that performs a task and is dependent on a class or object. It can access the state of the object or class.

Example of a Method:

class Counter {
    private int count = 0;

    public void increment() {
        count++;
    }

    public int get() {
        return count;
    }
}

Counter c1 = new Counter(); // create an object first
c1.increment(); // call the increment method twice
c1.increment();
int result1 = c1.get(); // call the get method to get the result

Counter c2 = new Counter();
c2.increment();
int result2 = c2.get();

Enter fullscreen mode Exit fullscreen mode

This class represents a counter and has one attribute count and two methods increment() and get(). The increment() method increments the count attribute by 1, and the get() method returns the current value of the count attribute.

We then create two Counter objects named c1 and c2, respectively. The c1 calls the increment() method twice and then calls the get() method and the c2 calls the increment() method once and then calls the get() method. The values of result1 should be 2, and result2 should be 1, respectively.

This posting is just a study note which was written after watching youtube videos in Korean.

Youtube video what I watched

Top comments (0)