Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

What is class and object in Java and OOP? Example

Class and Object are two pillars of Java and any Object oriented programming language. There are multiple ways you can describe Class in Java, I will start with how I remember class from my college Days. That time, I used to remember that "A class is a blueprint to create object" and that's how I answer during viva or exam. While I know what is a blueprint, I didn't really understand what a class can do. Later, when I started coding, I learned about data types like int data type to hold integer values. String data type to hold text values.  At that time, I understood  that a Class is a user defined type, you can create a structure of a user defined type like Book which can have a String author, number of pages as integer, and price as double. That's your blueprint to create Book objects which provides values for those variables. 

 In a nutshell both the definition of class and object are correct. For beginner programmers it's blueprint to create objects and for few of other it's just a way to model real world entity. 

Questions come why do we need Class in Java, well one thing I can think of is to combine multiple types into one structure. Class and Object along with core OOPS concept like Abstraction, Encapsulation, Inheritance and Polymorphism is the first thing any Java programmer learn.   



An Example of Class and Object in Java

Now, let's see an example of a class and object in Java to understand the concept better. We will first create a Book class and then a couple of book objects using that class. Remember, in Java a class name starts with a capital letter and a public class is saved with same file name as class name. 

In order to create a Class in Java we use "class" keyword and in order to create an object we use new keyword which calls the constructor  of the class to initialize new object. Initialization is nothing but assigning value to each member variable. For example, a Book class has a author, number of pages, title, and price when you create a new book object then you provide value to each of those member variable. 

Here is our Book class:

public class Book {

  private String author;
  private int numberOfPages;
  private String title;
  private double price;
  
  public Book(String author, int numberOfPages, String title, double price) {
    super();
    this.author = author;
    this.numberOfPages = numberOfPages;
    this.title = title;
    this.price = price;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public int getNumberOfPages() {
    return numberOfPages;
  }

  public void setNumberOfPages(int numberOfPages) {
    this.numberOfPages = numberOfPages;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public double getPrice() {
    return price;
  }

  public void setPrice(double price) {
    this.price = price;
  }
  
  
}


And, here is our Java program which creates two Book objects using Book class to print their title and author in console:

public class Library {
  
  public static void main(String args[]){
    
    Book headFirstJava = new Book("Kathy Sierra", 300, "Head First Java", 45.5d);
    Book effectiveJava = new Book("Joshua Bloch", 250, "Effective Java", 55.5d);
    
    System.out.println("First book Title: " + headFirstJava.getTitle());
    System.out.println("Second book Title: " + effectiveJava.getTitle());
  }

}


And here is the output:

First book Title: Head First Java
Second book Title: Effective Java


You can see that how we used class in Java to create a user defined variable to represent a real word thing like Book. You can similarly create any kind of class like Order, Trade, Item, Person, Employee, anything which your program needs. 



Difference between Class and Object in Java?

As I said, the key difference between class and object is that class is a blueprint and object is actual values which matters. Object are also called instance in Java.  Class definite structure and provide place holder which can hold values for object.

Here are few more differences between class and object for interviews

What is class and object in Java and OOP? Example



Things to remember about Class in Java

Following are some key thing about Class in Java programming language for beginners :

1) Class is represented using class keyword

2) There are two kinds of class, top level and nested class in Java.

3) According to Java convention, the name of the class should start with capital letter.

4) Unlike C structures which are analogous to class, Java classes can contain not only variables but also methods which operate on those fields.

5) The class is the smallest unit of modular code, functionality in Java is presented in terms of class.

6) As per SRP (Single Responsibility Principle)  SOLID design principle, a class should only represent one thing and there should not be more than one reason to change a class. Well designed classes are key for loosely coupled and modular code in Java.

6) Class doesn't do anything special, it's just defined structure and object do all the things.




That's all on What is Class in Java programming language, there are a lot more details which we will discuss in the further post.  The best way to learn Class and Object is to model some real world things like Person, Student, Employee etc.


Related Java and OOPS programming Tutorials
What is abstraction in Java programming language
What is Encapsulation in Java with Example
What is a polymorphism in Java? method overloading or overriding
What is Inheritance in Java with Example
Difference between Abstraction and Encapsulation in Java

1 comment:

  1. Nice explanation & specially videos are very beneficial

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.