<- back

🎯

Starting with Object-Oriented Programming



TL;DR

  • My first steps into Object-Oriented Programming with Java
  • Understanding classes as blueprints for creating objects
  • Learning about encapsulation, inheritance, and polymorphism
  • My confusion about where to draw the line between object responsibilities

I started diving into object-oriented programming (OOP) with Java, and honestly, it feels like a whole new level of coding.

So far, my goal is just stringing together lines of code freely and giving space to my creativity. But this week, I had to create these "objects", and each one has its own data and actions it can perform.


The first thing that started to make sense was the idea of a "class". Think of it like a blueprint. I can create a Dog class, and it has characteristics like breed, age, and name. It can also do things, like bark() or wagTail().

Then I can make a bunch of Dog objects, and each one can have its own breed, age, and name.


public class Dog {
    private String breed;
    private int age;
    private String name;
    
    public Dog(String breed, int age, String name) {
        this.breed = breed;
        this.age = age;
        this.name = name;
    }
    
    public void bark() {
        System.out.println(name + " says: Woof!");
    }
    
    public void wagTail() {
        System.out.println(name + " is wagging its tail!");
    }
}

I noticed that every time my teacher gives easy examples. Or maybe when she explains things, everything becomes easy... Anyway, what about putting into a class a "touch screen"? What's the function of that? Show images? Allow something else to touch on it? But is it the screen's function or is it someone else's hands function? I have no clues.


Then there's "encapsulation." It's like bundling everything related to an object together and keeping some of it private. It's supposed to be cleaner and easier to manage...


"Inheritance" is pretty cool too. It means one class can inherit stuff from another class. So, a BrazilianFila class can inherit all the basic GuardBreed stuff, but also have its own unique characteristics. It's like building on top of what's already there.


public class GuardBreed extends Dog {
    public void guard() {
        System.out.println(name + " is guarding the property!");
    }
}

public class BrazilianFila extends GuardBreed {
    public void loyalProtection() {
        System.out.println(name + " shows extreme loyalty to family!");
    }
}

And finally, "polymorphism." This one's a bit trickier, but it's about objects being able to take on different forms. So, if I have a method that expects a GuardBreed, I can also pass it a BrazilianFila because a BrazilianFila is a GuardBreed.


Let's continue!



<- Back to blog


All rights not reserved. Do you want a website like this? Just copy it 👍🏾