day-3

Classes and Objects

Class

A class is a blueprint to represent anything in the real world. Classes are made of 2 important concepts. Properties/Characteristics and behaviors.

Object

Object is the real thing. For example from a Person class we can create multiple people.

Example

package com.amigoscode.person;

import com.amigoscode.Gender;
import com.amigoscode.address.Address;

import java.util.List;
import java.util.Objects;

public class Person {
    private String name;
    private String email;
    private String phoneNumber;
    private Gender gender;
    private Address address;
    private List<Address> previousAddresses;

    public Person(String name,
                  String email,
                  String phoneNumber,
                  Gender gender) {
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", gender=" + gender +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name) && Objects.equals(email, person.email) && Objects.equals(phoneNumber, person.phoneNumber) && gender == person.gender;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, email, phoneNumber, gender);
    }
}

Most the code above can be generate using IntelliJ or you favorite IDE.

Properties

Constructor

Used to set the initial values for any given object

Getters

Used to set a particular property value

Setters

ToString

Used to print the string representation of an Object

Equals and Hashcode

The java equals hashcode are methods used to compare two Person objects

Examples

https://github.com/amigoscode/bright/tree/main/examples

Static

In the Java programming, the keyword static indicates that the particular member belongs to a class, rather than to an instance of that class.

To access a static field we can just refer to the class name followed by a dot then the static field.

There is no need to create an instance of Person to access the field count inside Person.

Inheritance

Inheritance is the process of inheriting attributes and methods from another class using the extends keywords.

There are two concepts:

  • Subclass (child) - the class that inherits from another class

  • Superclass (parent) - the class being inherited from

Creating class Animal and instantiating an object is to absract. Which animal? is it a dog or a cat?

Lets create a superclass called Animal and 2 subclass. One for Dog and another for Cat. Both dog and cat are animals.

Abstract Keyword

As mentioned above if we create an object of type Animal it does not really makes sense. To prevent anyone from creating objects using the class Animal we can add the keyword abstract as follows:

If you try the following code it will not compile.

However we can animals like this:

This works because both Cat and Dog extend Animal.

Interfaces

Last updated