Java Basics  «Prev  Next»

Lesson 1

Java Object-Oriented Programming

This course and the following modules discuss the Java language in the context of object oriented programming.

Course goals

This advanced course is designed to teach programmers with at least some experience with Java how to take advantage of Java's multitasking and networking features.
You will learn how to:
  1. Use advanced object-oriented features such as interfaces and packages
  2. Throw and catch exceptions
  3. Write multitasking programs
  4. Take advantage of the java.io and java.net packages

Inheritance with classes

When we discuss inheritance in the context of an object-oriented programming language such as Java, we talk about how a class can inherit the properties and behavior of another class. The class that inherits from another class can also define additional properties and behaviors. The Java Certification exam will ask you explicit questions about the need to inherit classes and how to implement inheritance using classes. Let us get started with the need to inherit classes.

Here's an example demonstrating encapsulation in Java using getters and setters:
public class Person {
    // Private fields to encapsulate data
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter for name
    public String getName() {
        return this.name;
    }

    // Setter for name
    public void setName(String name) {
        if (name != null && !name.isEmpty()) {
            this.name = name;
        } else {
            System.out.println("Name cannot be null or empty.");
        }
    }

    // Getter for age
    public int getAge() {
        return this.age;
    }

    // Setter for age with validation
    public void setAge(int age) {
        if (age > 0 && age < 150) {
            this.age = age;
        } else {
            System.out.println("Age must be between 1 and 149.");
        }
    }
}

// Example usage
public class Main {
    public static void main(String[] args) {
        Person person = new Person("Alice", 30);
        
        // Using getters
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());

        // Using setters
        person.setName("Bob");
        person.setAge(40);
        
        System.out.println("Updated Name: " + person.getName());
        System.out.println("Updated Age: " + person.getAge());

        // Trying to set invalid values
        person.setName("");
        person.setAge(200);
    }
}


Explanation:
  • Encapsulation is achieved by making the fields private, which means they can only be accessed within the class itself.
  • Getters (Accessors) provide read access to the fields. They are used to retrieve the value of private fields.
  • Setters (Mutators) provide write access to the fields. They can include logic for validation or transformation before setting the field's value.

In this example:
  • The setName method checks if the name is not null or empty before setting it.
  • The setAge method ensures that the age is within a reasonable range.

This approach helps in maintaining the integrity of the object's state and provides controlled access to the data.

JavaDeploy Series

The Introduction to Java Series begins with an introductory course, continues with a course on building graphical user interfaces, and culminates with additional courses on this website.

This series is intended to:
  1. Introduce students to the benefits and liabilities of designing in Java
  2. Lead programmers toward a mastery of the Java language

As a student progressing through this sequence, you will develop programs ranging from simple, character-mode applications to full-fledged, interactive applets that run directly on the World Wide Web.

SEMrush Software