Skip to main content

You simply understand Java Objects and Classes

A Trip to Objects and Classes

You simply understand Java Objects and Classes

Photo by Thought Catalog on Unsplash

Hello everyone, In this article, you will learn about Java Objects and Classes.

What is an Object in Java?

if we consider the real world, we can find objects around us. Examples, computer, car, dog, human, book, bag etc. All these objects have states and behavior.

if we consider a dog's state is colour, name, breed and its behaviour is barking, running, wagging.

Then Object is an entity that has state and behaviour. An object has an identity. it’s typically implemented in Unique id.

An Object is an instance of the class. A class template from which objects are created.

What is a Class in Java?

A class is a blueprint from which individual objects are created.

Syntax of the class


class <class_name>{
   field;
   method;
}

Thinking about Objects

Photo by Jonathan Cosens Photography on Unsplash

Instance Variables of Java

Things an object knows about itself are called Instance Variables.

Methods of Java

Things an object can do are called methods.

Look at this picture you can better understand it.

Bates and Sierra, 2005

What’s the difference between a class and an Object

A class is not an object. The object is used to create a class. It tells the JVM how to make an object of that particular type. Each object made from that class can have a value for the instance variable.

Let’s make your first Object

You need two classes. one is a type of object you want to use. for example Dog, Book, Computer etc.) and another class is the tester class where you put the main method. The main method you created and access objects of your new class type

 

Look at the example

1. write your class

Bates and Sierra, 2005

2. Write the tester class whose name <someClassName>TestDrive to test your objects. For example, if you make a dog class. you will need DogTestDrive class

Bates and Sierra, 2005

3. You can create an object and access the object’s variables and methods.

Bates and Sierra, 2005

Let’s check your knowledge.


Put your answers to these questions and check the answers


Bates and Sierra, 2005

Answers.


Bates and Sierra, 2005

Conclusion

I hope, in this article, you go trip with me to know about objects and classes. I will cover Java in upcoming articles. stay on with me.

Reference

Bates, B. and Sierra, K., 2005. Head First Java, 2nd Edition. [S.l.]: O’Reilly Media, Inc.


Popular posts from this blog

How Object Behave?

  How Object Behave? Photo by Ellen Qin on  Unsplash Hello everyone, In This article, you will learn about how the state and how to relate to one another. We know about objects and variables. I give you a small reminder class that describes what an object knows and what an object does. A class is not an object. The object is used to create a class. It tells the JVM how to make an object of that particular type. Each object made from that class can have a value for the instance variable. Bates and Sierra, 2005 if you want to remind read my article first. links are here - A Trip to Objects and Classes , Know your Variables . It will help you. you already know that every object of that type can have a different instance variable. what about the method? Can objects of that type have different method behaviour? Every instance of a class has the same methods, but depending on the value of the instance variables, the methods can behave differently. Look at this example. Do...

The basics you need to know about Java Programming Language

  JAVA- Beginners need to know The basics you need to know about Java Programming Language Hello everyone, you are new to Java, This article is for you. This article great place to start learning JAVA. Let’s start What is the JAVA? Java is a Programming Language and platform-independent. It’s friendly syntax, object-oriented and has strong memory management and portability. The history of Java is very interesting. Read the history of Java by following the Link Click here —  History of Java . Platform : Any hardware or software environment in which a program runs is known as a platform. Java code can run on a variety of platforms, such as Windows, Linux, Sun Solaris, Mac OS, and others. You can understand more about it. Let’s see H ow the way Java Works Step 1 : Create a source code file. (with the .java extension) Source code means to hold on to class definition. class represent a piece of the program. the class has one or more methods. for example, In the student clas...

you better understand Java primitives and references

  Know Your Variables you better understand Java primitives and references Hello everyone, Today you will learn about what is primitives and references. Variables The variable is container holds the value while the Java program is executed. you have used variables in two places. The first one is the instance variable other one is the local variable. Later we will use variables as an argument and as a return type. you have seen the variable declare as a primitive type. Declaring the Variable Let’s see how to declare the variable. Variable must have the type and the name. Don’t mess up. you will understand it while you reading the article till the end. Let’s see what is the Primitive Datatypes The different sizes and values that can be stored in the variable are defined by data types. Data types have two types. primitive data types — These include boolean, char, byte, short, int, long, float and double. non-primitive data types — These include Classes, Interfaces, and Arrays. Data Ty...