Java101
  • Welcome
  • Videos πŸ–₯️
    • Video Links
  • Exercises
    • Assignment 1 - Tests - Χ”Χ‘Χ•Χ“Χ§
    • Exercise 2
    • Exercise 4
  • Lesson 1 - Intro
    • MyConsole
    • Variables, Conditionals and Exercises
    • More Exercises
    • IDE Shortcuts
  • Lesson 2
    • Lesson 2 - While
  • Lesson 3 - for loops
    • for loops
  • Lesson 4 - Arrays
    • Arrays
  • Lesson 5 - Functions and Multi-dim Arrays
    • Multidimensional arrays & Functions
  • Lesson 6 - Strings, JUnit and Recursion
    • Useful String functions and more
    • Strings
    • More Exercises
  • Lesson 7
    • Recursion
    • More Exercises
  • Lesson 8 - OOP
    • Intro to Classes and Objects
    • Exercise: Creating circles
    • Lesson 9 - More OOP
  • Lesson 9 - Linked Lists
    • Linked Lists
  • Lesson 10 - Interfaces
    • Interface - Intro
    • Interfaces - Exercises
  • Lesson 11 - Test Review
    • index
Powered by GitBook
On this page
  • Lesson 8 - OOP
  • What are Classes and objects?
  • Attempt 1 🐢
  • Attempt 2 - Adding constructors
  • Attempt 3 - Adding encapsulation
  • An array of Dogs πŸΆπŸ©πŸ•

Was this helpful?

  1. Lesson 8 - OOP

Intro to Classes and Objects

Lesson 8 - OOP

There are four main OOP (object oriented programming) concepts in Java.

  • Encapsulation (setters and getters, we will discuss this today)

  • Abstraction

  • Inheritance

  • Polymorphism

Today OOP topics

  • non static methods

  • this

  • constructors and copy constructors

  • objects are passed to functions as references

  • toString

OOP - Encapsulation

Χ”Χ‘ΧͺΧ¨Χͺ ΧžΧ™Χ“Χ’

  • public and private

  • getters and setters

What are Classes and objects?

  • Class - Dogs

  • Data members or objects- size, age, color, breed, etc.

  • Methods- sleep(), toString()

Attempt 1 🐢

// Class Declaration
public class Dog {
    // Instance Variables
    String breed;
    String name;
    String size;
    int age;
    String color;


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

    //static method
    public static void whoIsSleeping(Dog dog){
        System.out.println(dog.name + " is sleeping");
    }

    public String toString() {
        return ("Name : "+name+ "\nBreed is: "+breed+"\nSize is: "+size+"\nAge is: "+age+"\nColor is: "+color+ "\n");
    }


    public static void main(String[] args) {
        Dog maltese = new Dog();
        maltese.breed="Maltese";
        maltese.size="Small";
        maltese.age=2;
        maltese.color="white";

        maltese.sleep();

        System.out.println();
        System.out.println(maltese);

        maltese.name="fluffy";
        Dog.whoIsSleeping(maltese);
    }
}
null is sleeping

Breed is: Maltese
Size is: Small
Age is: 2
Color is: white

fluffy is sleeping
  • we have a static method (doesnt belong to class), whoIsSleeping()

  • we have a non static method (belongs to class), sleep()

We got null is slepping , we forgot to initialize the name we should use a constructor

Attempt 2 - Adding constructors

here we will add 2 constructors

  • a copy constructor

  • a constructor that receives arguments

public class Main {
    public static void main(String[] args) {
        Dog fluffy = new Dog("Fluffy","Maltese",
                "Small",2,"white");

        fluffy.sleep();

        System.out.println();
        System.out.println(fluffy);

        Dog snowball = new Dog(fluffy); //call copy constructor

        System.out.println();
        snowball.name = "Snowball";
        System.out.println(snowball.name);
    }
}
// Class Declaration
public class Dog {
    // Instance Variables
    String name;
    String breed;
    String size;
    int age;
    String color;

    //constructor - ADDED THIS
    Dog(String my_name, String breed, String size, int age, String my_color) {
        this.breed=breed;
        name=my_name;
        this.age = age;
        this.size=size;
        color= my_color;
    }

    //copy constructor -  ADDED THIS
    Dog(Dog other) {
        breed = other.breed;
        name = other.name;
        age = other.age;
        size = other.size;
        color = other.color;
    }

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

    //static method
    public static void whoIsSleeping(Dog dog){
        System.out.println(dog.name + " is sleeping");
    }

    public String toString() {
        return ("Name : "+name+ "\nBreed is: "+breed+"\nSize is: "+size+
                "\nAge is: "+age+"\nColor is: "+color+ "\n");
    }
}
// Class Declaration
public class Dog {
    // Instance Variables
    String name;
    String breed;
    String size;
    int age;
    String color;

    //constructor - ADDED THIS
    Dog(String my_name, String breed, String size, int age, String my_color) {
        this.breed=breed;
        name=my_name;
        this.age = age;
        this.size=size;
        color= my_color;
    }

    //copy constructor -  ADDED THIS
    Dog(Dog other) {
        breed = other.breed;
        name = other.name;
        age = other.age;
        size = other.size;
        color = other.color;
    }

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

    //static method
    public static void whoIsSleeping(Dog dog){
        System.out.println(dog.name + " is sleeping");
    }

   public String toString() {
        return ("Name : "+name+ "\nBreed is: "+breed+"\nSize is: "+size+"\nAge is: "+age+"\nColor is: "+color+ "\n");
    }


    public static void main(String[] args) {
        Dog fluffy = new Dog("Fluffy","Maltese",
                "Small",2,"white");

        fluffy.sleep();

        System.out.println();
        System.out.println(fluffy);

        Dog snowball = new Dog(fluffy); //call copy constructor

        System.out.println();
        snowball.name = "Snowball";
        System.out.println(snowball.name);
    }
}
Fluffy is sleeping

Breed is: Maltese
Size is: Small
Age is: 2
Color is: white

Snowball

Attempt 3 - Adding encapsulation

we should not be able to directly change and get properties of the object for example here

public static void main(String[] args) {
        Dog fluffy = new Dog( "Fluffy", "Maltese", "Small", 2, "white");
        fluffy.name = "Snowball";
    }
  • but rather use setters and getters

  • the fields should also be private (can't be accessed outside the class)

so now we have

public class Main {
    public static void main(String[] args) {
        Dog fluffy = new Dog("Fluffy","Maltese","Small",2,"white");

        Dog snowball = new Dog(fluffy); //call copy constructor

        System.out.println();
        snowball.setName("Snowball");
        System.out.println(snowball.getName());
    }
}
// Class Declaration
public class Dog {
    // Instance Variables
    private String name;
    private String breed;
    private String size;
    private int age;
    private String color;

    //constructor
    Dog(String my_name, String breed, String size, int age, String my_color) {
        this.breed=breed;
        name=my_name;
        this.age = age;
        this.size=size;
        color= my_color;
    }

    //copy constructor
    Dog(Dog other) {
        breed = other.breed;
        name = other.name;
        age = other.age;
        size = other.size;
        color = other.color;
    }

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

    //static method
    public static void whoIsSleeping(Dog dog){
        System.out.println(dog.name + " is sleeping");
    }

    //getters and setters
    // === ADDED THIS ===
    public String getName() {
        return name;
    }

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

    public String getBreed() {
        return breed;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }


    public String toString() {
        return ("Name : "+name+ "\nBreed is: "+breed+"\nSize is: "+size+"\nAge is: "+age+"\nColor is: "+color+ "\n");
    }
}
Snowball

An array of Dogs πŸΆπŸ©πŸ•

public class Main {
    public static void main(String[] args) {
        String[] names = {"fluffy", "snowball", "shugi", "cookie"};
        Dog[] dogs = new Dog[4];

        for (int i = 0; i < dogs.length; i++)
            dogs[i] = new Dog(names[i],"Maltese","Small",2,"white");

        for (int i = 0; i < dogs.length; i++)
            System.out.println(dogs[i]);

    }
}
Name : fluffy
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white

Name : snowball
Breed is: Maltese
Size is: Small
Age is: 2
Color is: white

...
PreviousMore ExercisesNextExercise: Creating circles

Last updated 4 years ago

Was this helpful?

Objects and Classes in Java
What is Class and Object in Java OOPS? Learn with Example