no

Learn Java Programming : Lesson 6 - Object-oriented Programming Concepts With Classes and Object

Go Back to Course Outline I. Introduction OOP or Object-Oriented Programming is about creating objects that contain both attribut...


Go Back to Course Outline

I. Introduction

  • OOP or Object-Oriented Programming is about creating objects that contain both attributes and methods that perform operations. Aside from Java other popular OOP languages are C++ and Python.
  • On the other hand, Procedural Programming is about writing procedures/routines/methods/functions that perform an operation on data. Popular examples would be PHP, C, Typescript, Javascript, etc.
  • Basically, there are 4 pillars of Object-Oriented Programming namely inheritance, polymorphism, abstraction and encapsulation. But before that let’s take a look at classes and objects first.

II. Class

  • Class is the template or blueprint for creating an object and everything in Java is associated with them.
  • A class would normally contain a constructor (always), attributes and methods.
  • To create a class, we would use the keyword “class”.

III. Object

  • To instantiate a class and create an object, we will use the keyword “new”.
  • A class is instantiated by calling the constructor of the class.
  • A constructor with no argument is available by default.
Running the jar in the terminal.
java -cp java-programming-beginner-0.0.1-SNAPSHOT.jar com.czetsuyatech.lesson6.Account

IV ToString, HashCode, Equals

  • By default, all class defined in Java extends the object class which makes several methods available such as toString, hashCode and equals.
  • equals - it compares whether 2 objects instantiated from the same class are equal. This is different compared to the “==” operator when comparing reference types as it compares whether the 2 objects refer to the same object in the memory.
  • hashcode - returns a hash code value (int) of the object.
    • Hash code value is always the same on the same object.
    • If objects a and b are equals, then they should have the same hash code.
    • Invoking hashcode with the same result does not necessarily mean that they are equal. This is due to the limitation of the value available in int.
  • toString - is the string representation of the object

V. Defining Methods

  • A method or a function in Java is just a block of code that performs certain actions when called.
  • Data can be pass to a method via parameter.
  • Why define a method? For code reuse, as they said to define a method once and use it multiple times.
  • The void method returns nothing.
  • A variable declared inside a method is called local, it is stored in a temporary state.

VI. Pass by Value

  • Everything in Java is passed by value.
  • Unfortunately, when passing an object it passes the reference to that object. Thus changing the value of the object inside a method, change the value in the caller as well. This is indeed, confusing for beginners.
For example, when defining an account:
Account account; ← this statement creates a new pointer to the Account. So,
Account account = new Account(“Sarrah”);
passByValue(account); ← means we are passing the pointer to the account object. See my previous video on how objects are stored in the memory.

VII. Class vs Instance

  • A class variable is another name for a static field.
    • Example static int x = 10;
  • Instance variable is another name for non-static field.
    • Example int x = 10;
 
Static/Class variable
Instance variable
Declared with a static keyword inside a class but outside a method, constructor or any block.
Declared inside a class but outside a method, constructor or any block.
Created when the program starts and destroyed on termination.
Created when a new object is created via “new” keyword.
It can be accessed by calling the class name. No need to initialize a class.
It can be accessed by calling the object declaration name.
Only one copy of each class variable per class.
Can hold multiple values per class. Different values per object.

Related

java-getting-started 1511944831336305117

Post a Comment Default Comments

item