<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oleksandr</title>
    <description>The latest articles on DEV Community by Oleksandr (@oleksandr_java).</description>
    <link>https://dev.to/oleksandr_java</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2883433%2F40f7aa09-d80d-4886-95e2-68a64c465fe2.png</url>
      <title>DEV Community: Oleksandr</title>
      <link>https://dev.to/oleksandr_java</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oleksandr_java"/>
    <language>en</language>
    <item>
      <title>Java: Inheritance, Method Overrides, and Polymorphism.</title>
      <dc:creator>Oleksandr</dc:creator>
      <pubDate>Fri, 09 May 2025 10:07:24 +0000</pubDate>
      <link>https://dev.to/oleksandr_java/java-inheritance-method-overrides-and-polymorphism-cao</link>
      <guid>https://dev.to/oleksandr_java/java-inheritance-method-overrides-and-polymorphism-cao</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my previous post, I covered encapsulation, getters and setters, access modifiers. Now let's move on to the following fundamental concepts of &lt;strong&gt;object-oriented programming: inheritance, method overrides, and polymorphism.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A subclass inherits all the members (fields, methods, and nested classes) from its superclass.&lt;/strong&gt; Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Inheritance:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Super class
class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }

    public void eat() {
        System.out.println(name + " is eating.");
    }

    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

// Subclass
class Dog extends Animal {

    public Dog(String name) {
        super(name); // Calling super class constructor
    }

    public void bark() {
        System.out.println(name + " is barking.");
    }
}

// Entry point
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Rex");

        myDog.eat();     // Inherited from Animal
        myDog.sleep();   // Inherited from Animal
        myDog.bark();    // Method of the Dog class
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rex is eating.
Rex is sleeping.
Rex is barking.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Facts About Inheritance in Java
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inheritance creates an “is-a” relationship&lt;/strong&gt;
If Dog extends Animal, then a Dog is an Animal&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The&lt;/strong&gt; &lt;code&gt;extends&lt;/code&gt; &lt;strong&gt;keyword is used&lt;/strong&gt;
Use &lt;code&gt;extends&lt;/code&gt; to create a subclass: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;class Dog extends Animal { ... }&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A subclass inherits everything except private members&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use&lt;/strong&gt; &lt;code&gt;super&lt;/code&gt; &lt;strong&gt;to access parent methods or constructors&lt;/strong&gt;
&lt;code&gt;super()&lt;/code&gt; calls the parent constructor.
&lt;code&gt;super.methodName()&lt;/code&gt; calls a parent class method.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In Java a class can extend only one other class.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid deep inheritance hierarchies&lt;/strong&gt;
Too many levels of inheritance can make code harder to understand and maintain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Method Overriding
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Method Overriding?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This allows a subclass to &lt;strong&gt;define specific behavior&lt;/strong&gt; while still using the general structure of the superclass.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To override a method:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method &lt;strong&gt;must exist in the superclass&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The method in the subclass &lt;strong&gt;must have the same name, return type, and parameters.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use the&lt;/strong&gt; &lt;code&gt;@Override&lt;/code&gt; &lt;strong&gt;annotation&lt;/strong&gt; (optional but recommended for clarity and compiler checks).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of Method Overriding&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
    public void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Polymorphism in action
        myAnimal.sound(); // Calls overridden method in Dog
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dog barks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Points About Method Overriding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The method must have the same signature as the one in the superclass&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The overridden method can have a more specific access modifier&lt;/strong&gt; (e.g., &lt;code&gt;protected&lt;/code&gt; → &lt;code&gt;public&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can use&lt;/strong&gt; &lt;code&gt;super.methodName()&lt;/code&gt; &lt;strong&gt;to call the superclass version of the method from the subclass.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overriding enables runtime polymorphism&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When Should You Override?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When a subclass needs to customize or specialize a method's behavior.
Example: An Animal class with a &lt;code&gt;sound()&lt;/code&gt; method, overridden by Dog, Cat, Cow, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What is Polymorphism?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Polymorphism means “many forms”. In Java, it allows one interface to be used for a general class of actions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With polymorphism, a parent class reference can point to a child class object, and the correct method is chosen at runtime.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a powerful concept because it allows you to write flexible and reusable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Polymorphism in Java:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
    public void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal1 = new Dog();
        Animal myAnimal2 = new Cat();

        myAnimal1.makeSound(); // Dog barks
        myAnimal2.makeSound(); // Cat meows
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dog barks  
Cat meows

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Points About Polymorphism
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Polymorphism allows methods to behave differently based on the object that’s calling them.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It works with method overriding — the subclass method overrides the parent version&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enables code generalization: one method can accept many types of objects.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Achieved through inheritance + method overriding.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Only methods are polymorphic — fields are not.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Use Polymorphism?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simplifies code and reduces duplication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Makes your code more extensible and maintainable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encourages the use of interfaces and abstract classes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Whats next
&lt;/h2&gt;

&lt;p&gt;In the upcoming posts, we’ll explore more advanced and practical concepts in Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Abstract Classes &amp;amp; Interfaces&lt;/li&gt;
&lt;li&gt;Exception Handling&lt;/li&gt;
&lt;li&gt;Enums and Constants &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;To reinforce what you’ve learned in this post:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create your own class hierarchy. For example,&lt;/strong&gt; &lt;code&gt;Animal&lt;/code&gt; → &lt;code&gt;Bird&lt;/code&gt;, &lt;code&gt;Fish&lt;/code&gt;&lt;strong&gt;, and&lt;/strong&gt; &lt;code&gt;Reptile&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Override a method like&lt;/strong&gt; &lt;code&gt;makeSound()&lt;/code&gt; &lt;strong&gt;in each subclass with different output.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Try calling overridden methods from the main class using different subclass objects.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;This will help you better understand inheritance, method overriding, and polymorphism in action.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>oop</category>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Java: Encapsulation, Getters and Setters, Access Modifiers</title>
      <dc:creator>Oleksandr</dc:creator>
      <pubDate>Fri, 21 Mar 2025 15:27:07 +0000</pubDate>
      <link>https://dev.to/oleksandr_java/java-encapsulation-getters-and-setters-access-modifiers-ihn</link>
      <guid>https://dev.to/oleksandr_java/java-encapsulation-getters-and-setters-access-modifiers-ihn</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my previous post, I covered constructors, return methods, and comments — essential concepts in Java. Today, we’ll continue exploring &lt;strong&gt;Object-Oriented Programming (OOP)&lt;/strong&gt; by learning about &lt;strong&gt;encapsulation, getters and setters, access modifiers, and why modifying values directly isn’t a good practice.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation in Java
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Encapsulation&lt;/strong&gt; is used to &lt;strong&gt;hide the values or state&lt;/strong&gt; of a structured data object inside a class, &lt;strong&gt;preventing unauthorized direct access&lt;/strong&gt; to them. Instead of allowing values to be changed freely, we &lt;strong&gt;use getters and setters&lt;/strong&gt; to control access and ensure data integrity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use encapsulation?
&lt;/h3&gt;

&lt;p&gt;✅ Keeps data safe by limiting access.&lt;br&gt;
✅ Provides better control over how data is modified.&lt;br&gt;
✅ Makes your code easier to maintain.&lt;/p&gt;
&lt;h2&gt;
  
  
  Access Modifiers
&lt;/h2&gt;

&lt;p&gt;Access modifiers define the visibility of classes, methods, and variables. Java has four main access modifiers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;public&lt;/code&gt;: Accessible from &lt;strong&gt;anywhere&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;default&lt;/code&gt;: Accessible within the &lt;strong&gt;same package&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;protected&lt;/code&gt;: Accessible within the &lt;strong&gt;same package and by subclasses&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;private&lt;/code&gt;: Accessible only within &lt;strong&gt;same class&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of Encapsulation with Getters and Setters:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    // Private fields to ensure data protection
    private String brand;
    private int maxSpeed;

    // Constructor to initialize values
    public Car(String brand, int maxSpeed) {
        this.brand = brand;
        this.maxSpeed = maxSpeed;
    }

    // Getter method for the brand
    public String getBrand() {
        return brand;
    }

    // Setter method for the brand
    public void setBrand(String brand) {
            this.brand = brand;
    }

    // Getter method for maxSpeed
    public int getMaxSpeed() {
        return maxSpeed;
    }

    // Setter method for maxSpeed
    public void setMaxSpeed(int maxSpeed) {
            this.maxSpeed = maxSpeed;
    }

    public void outputValues() {
        System.out.println("Brand: " + brand);
        System.out.println("Max Speed: " + maxSpeed);
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("BMW", 250);

        // Using getter to display values
        System.out.println("Initial brand: " + car.getBrand());

        // Using setter to update values
        car.setBrand("Audi");
        car.setMaxSpeed(300);

        // Display updated values
        car.outputValues();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Initial brand: BMW
Brand: Audi
Max Speed: 300
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Avoid Direct Value Modification?
&lt;/h2&gt;

&lt;p&gt;Directly changing field values bypasses important checks and logic that ensure data consistency. Using getters and setters allows you to:&lt;/p&gt;

&lt;p&gt;✅ Add validation rules.&lt;br&gt;
✅ Control how values are updated.&lt;br&gt;
✅ Improve code flexibility and scalability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;✅ Encapsulation protects your data by using private fields.&lt;br&gt;
✅ Getters and setters help control how data is accessed and modified.&lt;br&gt;
✅ Access modifiers define what can be accessed from where.&lt;/p&gt;

&lt;h2&gt;
  
  
  Whats next
&lt;/h2&gt;

&lt;p&gt;In the next post, we'll explore the topic of inheritance, method overriding, and polymorphism in Java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To practice, I suggest writing your own class with private fields, using both getters and setters. Try experimenting with access modifiers to understand their behavior better.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Java: Constructors, Return Methods, Comments</title>
      <dc:creator>Oleksandr</dc:creator>
      <pubDate>Fri, 21 Feb 2025 17:29:32 +0000</pubDate>
      <link>https://dev.to/oleksandr_java/java-constructors-return-methods-comments-1oeh</link>
      <guid>https://dev.to/oleksandr_java/java-constructors-return-methods-comments-1oeh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my previous post, I covered Objects, Methods, and Output—key concepts in Java. Today, we’ll continue exploring &lt;strong&gt;Object-Oriented Programming (OOP)&lt;/strong&gt; by diving into &lt;strong&gt;сonstructors, methods with return values, comments&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Comments in Java
&lt;/h2&gt;

&lt;p&gt;Comments are a way to write notes for yourself or provide explanations for other programmers within your code. They are ignored by the compiler when the code runs, meaning they don’t affect the program’s execution—they are only for humans to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is compiler?
&lt;/h3&gt;

&lt;p&gt;A compiler is a program that translates your Java code into a format that the computer can understand and execute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java supports different types of comments:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single-line comment (starts from &lt;code&gt;//&lt;/code&gt;), used for short notes or explanations.
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Next line print car's brand name
System.out.println("Brand name: " + brandName); // Remember about ';' 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Multi-line comments(starts from &lt;code&gt;/*&lt;/code&gt; and ends with &lt;code&gt;*/&lt;/code&gt;), used when you need to write longer explanations.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*
The following line prints the car's model.
Multi-line comments are useful for detailed explanations.
*/
System.out.println("Model: " + model);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constructors in Java
&lt;/h2&gt;

&lt;p&gt;In simple terms, a constructor in Java is a special method used to create (initialize) objects. It runs automatically when an object of a class is created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some key points about constructors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The constructor has the same name as the class&lt;/li&gt;
&lt;li&gt;It does not have a return type&lt;/li&gt;
&lt;li&gt;It is used to set initial values for object properties(variables)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of a constructor:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String brandName;
    String model;
    int maxSpeed;
    double length;
    boolean isEngineWorks;

    // Constructor
    Car() { 
        // Initialize all variables in the constructor
        brandName = "BMW";
        model = "M5";
        maxSpeed = 250;
        length = 4.983;
        isEngineWorks = true;
    }

    void outputValues() {
        System.out.println("Brand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Works: " + isEngineWorks);
    }
    public static void main(String[] args) {
        Car car = new Car(); // Calling the constructor when creating an object
        car.outputValues();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Works: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;If you do not write any constructor for a class, Java will automatically use a default constructor.&lt;br&gt;
However, the default constructor does not initialize variables - it only creates an object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Constructors in Java can also accept parameters to initialize variables dynamically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of a constructor with parameters:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String brandName;
    String model;
    int maxSpeed;
    double length;
    boolean isEngineWorks;

    // Constructor with parameters
    Car(String brandName, String model, int maxSpeed, double length, boolean isEngineWorks) {
        this.brandName = brandName;
        this.model = model;
        this.maxSpeed = maxSpeed;
        this.length = length;
        this.isEngineWorks = isEngineWorks;
    }

    void outputValues() {
        System.out.println("Brand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Works: " + isEngineWorks);
    }

    public static void main(String[] args) {
        // Passing values to the constructor
        Car car = new Car("BMW","M5",250,4.983,false); 
        car.outputValues();

    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Works: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Keyword &lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
In the previous example, you might have noticed the use of &lt;code&gt;this&lt;/code&gt;.&lt;br&gt;
The &lt;code&gt;this&lt;/code&gt; keyword refers to the current object’s variables.&lt;br&gt;
It helps differentiate between class variables and constructor parameters when they have the same name.&lt;/p&gt;
&lt;h2&gt;
  
  
  Methods with parameters
&lt;/h2&gt;

&lt;p&gt;Like constructors, methods can also receive parameters that we can use inside them.&lt;br&gt;
&lt;strong&gt;Example of a method with parameters:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.printMessage("Hello");
    }

    // Method with parameters message
    void printMessage(String message) {
        System.out.println(message);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Method with return type
&lt;/h2&gt;

&lt;p&gt;In Java, a method with a return type is a method that returns a value after execution. The return type specifies the type of data the method will return.&lt;br&gt;
&lt;strong&gt;Example of a method with return type:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        int a= main.returnInt();
        String b= main.returnString();
        boolean c= main.returnBoolean();
        double d= main.returnDouble();

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }

    int returnInt() {
        return 1;
    }

    String returnString() {
        return "Hello";
    }

    boolean returnBoolean() {
        return true;
    }

    double returnDouble() {
        return 1.0;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
Hello
true
1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;You can also use return value for simple output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.returnString());
    }

    String returnString() {
        return "Hello";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note that a method can also have a &lt;code&gt;void&lt;/code&gt; return type if it does not need to return a value.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Keyword &lt;code&gt;return&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;return&lt;/code&gt; keyword specifies which value Java should return as the result of the method execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;✅ Constructors help us to initialize objects.&lt;br&gt;
✅ Methods can return values using the &lt;code&gt;return&lt;/code&gt; keyword.&lt;br&gt;
✅ Comments make code more understandable.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;p&gt;In the next post, we'll discover the topic of encapsulation, Getters and Setters(practical use of encapsulation), access modifiers and why we should not change values directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In order to better learn the material, I advise you to write your own class that would contain methods with return types, default and parameterized  constructors (try to use them both to create two different objects) and also practice with comments.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Getting Started with Java: Objects, Methods, and Output</title>
      <dc:creator>Oleksandr</dc:creator>
      <pubDate>Thu, 20 Feb 2025 17:01:40 +0000</pubDate>
      <link>https://dev.to/oleksandr_java/getting-started-with-java-objects-methods-and-output-4a6c</link>
      <guid>https://dev.to/oleksandr_java/getting-started-with-java-objects-methods-and-output-4a6c</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my previous post, I shared my journey and goals as I work towards becoming a Java Developer. Now, it’s time to dive into actual coding!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java is an object-oriented programming (OOP) language&lt;/strong&gt;, meaning everything revolves around objects. But why is this important? Think of it like math: you can’t solve logarithms without first understanding basic operations like addition and subtraction. The same applies to Java—before building complex applications, you must grasp the fundamentals.&lt;/p&gt;

&lt;p&gt;Today, we’ll cover three core concepts in Java: &lt;strong&gt;objects, methods, and fields&lt;/strong&gt;. We’ll also learn how to display output in the console using &lt;code&gt;System.out.print()&lt;/code&gt;. If you're just starting out, this post will give you a solid foundation. And if you already know Java, consider it a refresher&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects in Java
&lt;/h2&gt;

&lt;p&gt;In Java, we create objects using the &lt;code&gt;class&lt;/code&gt; keyword. A class serves as a blueprint for creating objects. Think of it like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Car is a class because it represents a general category.&lt;/li&gt;
&lt;li&gt;Specific cars like BMW, Volvo, or Toyota are objects that belong to this class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every car has properties such as model name, number of seats, max speed, and color. In Java, these properties are called &lt;strong&gt;fields (variables)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declaring Fields (Variables) in Java
&lt;/h2&gt;

&lt;p&gt;Here’s how we define different types of variables in Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Integer Numbers (Whole Numbers):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;byte&lt;/code&gt; (from -128 to 127)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;short&lt;/code&gt;(from -32,768 to 32,767)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;int&lt;/code&gt;(from -2,147,483,648 to 2,147,483,647)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;long&lt;/code&gt;(from -9 quintillion to +9 quintillion)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Decimal Numbers (Floating-Point Numbers):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;float&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;double&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Single Character:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Boolean Values (true/false):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Text (Strings):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String (text enclosed in double quotes " ")&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Let's create a simple &lt;code&gt;Car&lt;/code&gt;class with some fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String brandName = "BMW";
    String model = "M5";
    int maxSpeed = 250;
    double length = 4.983;
    boolean isEngineRunning = false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding Field Declaration
&lt;/h2&gt;

&lt;p&gt;Each field declaration consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A data type (String, int, double, etc.)&lt;/li&gt;
&lt;li&gt;A variable name (brandName, model, etc.)&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;assignment operator&lt;/strong&gt; = (used to assign values)&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;semicolon&lt;/strong&gt; ; (required by Java syntax to end a statement)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Rules for Naming Fields in Java
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Can contain letters (A-Z, a-z), numbers (0-9), underscores _, and dollar signs $ (though $ is rarely used in practice)&lt;/li&gt;
&lt;li&gt;Cannot start with a number (e.g., 3speed ❌)&lt;/li&gt;
&lt;li&gt;Cannot contain spaces (e.g., max speed ❌)&lt;/li&gt;
&lt;li&gt;Cannot use Java keywords (e.g., int, class, etc.)&lt;/li&gt;
&lt;li&gt;Follows camelCase convention (e.g., brandName, isEngineRunning)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Output Information in Java
&lt;/h2&gt;

&lt;p&gt;To display information in the console, we use &lt;code&gt;System.out.print()&lt;/code&gt; and &lt;code&gt;System.out.println()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;System.out.print()&lt;/code&gt; → Prints text without moving to a new line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;System.out.println()&lt;/code&gt; → Prints text and moves to the next line.
Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;System.out.println("Hello");
System.out.print("World!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also print variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String brand = "BMW";
System.out.println("Car brand: " + brand);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car brand: BMW
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;+&lt;/code&gt; operator is used to concatenate (join) text and variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods in Java
&lt;/h2&gt;

&lt;p&gt;Objects don’t just store data—they also perform actions! These actions are defined using methods.&lt;/p&gt;

&lt;p&gt;For example, a car can start the engine, accelerate, or brake. In Java, we define these actions as methods inside the class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String brandName = "BMW";
    String model = "M5";
    int maxSpeed = 250;
    double length = 4.983;
    boolean isEngineRunning = false;

    void printCarInfo() {
        System.out.println("Brand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Running: " + isEngineRunning);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To define a method, use the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ReturnType] methodName() {
    // method body
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Main Method: The Entry Point
&lt;/h2&gt;

&lt;p&gt;A Java application requires a main method as its entry point. Java starts execution from the main method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
class Car {
    String brandName = "BMW";
    String model = "M5";
    int maxSpeed = 250;
    double length = 4.983;
    boolean isEngineRunning = false;

    public static void main(String[] args) {
        Car car = new Car();
        car.printCarInfo();
    }

    void printCarInfo() {
        System.out.println("Brand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Running: " + isEngineRunning);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Running: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modifying Object Fields with Methods
&lt;/h2&gt;

&lt;p&gt;We can also modify object properties using methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String brandName = "BMW";
    String model = "M5";
    int maxSpeed = 250;
    double length = 4.983;
    boolean isEngineRunning = false;

    public static void main(String[] args) {
        Car car = new Car();
        car.printCarInfo();

        car.changeValues();
        car.printCarInfo();
    }

    void printCarInfo() {
        System.out.println("\nBrand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Running: " + isEngineRunning);
    }

    void changeValues() {
        brandName = "Mercedes";
        model = "GLE";
        maxSpeed = 230;
        length = 4.926;
        isEngineRunning = true;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Running: false

Brand name: Mercedes
Model: GLE
Max Speed: 230
Length: 4.926
Engine Running: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Summary&lt;br&gt;
Today, we covered:&lt;br&gt;
✅ Objects: The building blocks of Java programs&lt;br&gt;
✅ Fields (Variables): Storing data inside objects&lt;br&gt;
✅ Methods: Defining actions for objects&lt;br&gt;
✅ Printing Output: Using System.out.print() and System.out.println()&lt;br&gt;
✅ The Main Method: The starting point of a Java program&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s Next?&lt;/strong&gt;&lt;br&gt;
In the next post, we'll dive deeper into constructors, a special type of method that helps us create objects efficiently!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
Mastering Java starts with understanding the basics. I hope this post gave you a clear introduction to objects, fields, methods, and printing output in Java. If you have any questions, feel free to ask in the comments!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In order to better learn the material, I advise you to write your own class that would contain the main method, several fields of different types, and methods for output and changing these fields&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My Journey to Becoming a Java Developer</title>
      <dc:creator>Oleksandr</dc:creator>
      <pubDate>Wed, 19 Feb 2025 18:07:12 +0000</pubDate>
      <link>https://dev.to/oleksandr_java/my-journey-to-becoming-a-java-developer-khd</link>
      <guid>https://dev.to/oleksandr_java/my-journey-to-becoming-a-java-developer-khd</guid>
      <description>&lt;p&gt;Hello everyone, I’m Oleksandr!&lt;br&gt;
I’m currently a third-year student at a university in Ukraine, but right now, I’m living in Zurich, Switzerland. I’m studying computer science, and my passion for programming started back in high school (around 9th grade). My first steps into the world of coding began with C++, and while I found it fascinating, I hit a roadblock when I encountered arrays. Unfortunately, I gave up on programming for a while after that.&lt;/p&gt;

&lt;p&gt;The story doesn’t end there, though. I came back stronger, diving into HTML and CSS. But, once again, I didn’t stick with it long enough to see the bigger picture.&lt;/p&gt;

&lt;p&gt;Fast forward to my first year at university, and I discovered Java — my specialization subject. I quickly became the top student in my group. However, when my university switched to online learning, it completely killed my motivation. I found myself playing games instead of studying, and when classes went back offline, I struggled to regain my rhythm.&lt;/p&gt;

&lt;p&gt;But this time, I didn’t give up. I reignited my motivation and threw myself back into Java. I started building simple projects and even had some success along the way. But when summer break came, my motivation dropped again, and I lost focus.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yet, I broke the cycle.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, I’m diving into new technologies like Spring Boot, Hibernate, Docker, and more. I’ve completed a few projects, including a social media website and a simple e-commerce site, and now I’m working on a budget tracker. I’m also planning to integrate AI features, which is both challenging and exciting!&lt;/p&gt;

&lt;p&gt;Despite my progress, I’ve realized that I still have gaps in my understanding of Java basics and algorithms. So, I’ve decided to start from scratch — reviewing the fundamentals of Java while continuing to develop my projects. This blog will be my way of documenting the journey, sharing my successes, struggles, and everything in between.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s next?&lt;/strong&gt;&lt;br&gt;
In this blog, I’ll share my experiences, lessons learned, and the steps I take to fill in those gaps in my knowledge. I’ll also talk about the projects I’m working on and how I apply the things I learn. Hopefully, my journey will inspire others who are on a similar path!&lt;/p&gt;

&lt;p&gt;I’d love for you to follow along as I continue this learning adventure. Feel free to share your thoughts, challenges, or advice — we can support each other on this journey!&lt;/p&gt;

</description>
      <category>aboutme</category>
    </item>
  </channel>
</rss>
