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?

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

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

  • Methods- sleep(), toString()

Attempt 1 🐶

  • 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

Attempt 3 - Adding encapsulation

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

  • but rather use setters and getters

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

so now we have

An array of Dogs 🐶🐩🐕

Last updated

Was this helpful?