Java Fundamentals  «Prev  Next»
Lesson 7

Class Object Conclusion

In this module, you discovered how to create objects from classes. You also learned more about inheritance and found out how to override and overload methods.

How do you create Objects from Classes in Java

To create an object of a class in Java, you use the new operator followed by the class constructor. Here is an example:
public class Demo {
  int x;
  
  public Demo() {
    x = 5;
  }
}

// creating an object of Demo
Demo myObj = new Demo();

This will create an object of the Demo class and assign the default value of 5 to the x field of the object. You can also pass arguments to the constructor to set the initial values of the object's fields:
public class Demo1 {
  int x;
  
  public Demo1(int x) {
    this.x = x;
  }
}

// creating an object of Demo1 and setting the value of x to 10
Demo1 myObj = new Demo1(10);

Glossary Terms

This module discussed how the following terms relate to Java:
  1. Object class: The Object class is the class that forms the root of the Java class hierarchy. The Object class serves as the basis for all Java classes, including the classes in the Java API.
  2. Override: To override is to replace an inherited method with a newer version
  3. Overload: To overload is to use the same name for several items in the same scope.

Working With Objects Classes - Quiz

Click the Quiz link to test what you have learned in this module.
Working With Objects Classes - Quiz