If you are new to Java and Object-Oriented Programming (OOP), this article helps you grasp the two key concepts in Java OOP: Classes and Objects. Else, I will help you revise how classes and objects are implemented in the Java programming language.

And to help you digest the content easily, this article is represented in form of the following 17 questions and answers:

1. What is a class?

2. What is structure of a class?

3. What is an object?

4. How many objects can you create?

5. Where are objects stored?

6. Do you need to destroy objects?

7. What is a top-level class?

8. Can you mark a class as static?

9. Which modifiers can you apply for a top-level class?

10. What is a nested class?

11. What is an inner class?

12. What is an abstract class?

13. What is a final class?

14. How can you create a new instance of an inner class?

15. What is a static nested class?

16. What is a method-local inner class?

17. What is an anonymous inner class?

Now, let's go through each question/answer in detailed.

 

1. What is a class?

A class is a blueprint or a design for a specific type of objects. In other words, a class abstracts real-world objects into a type in terms of OOP. For example, the class Dog abstracts all types of dogs in the real world; the class Computer represents computers in the real world.

A real-world object has characteristics and behaviors, e.g. a dog has white hair and it can bark. So in programming, a class abstracts that into attributes (fields) and functions (methods).

So in OOP, a class is the smallest, independent unit you have to create. A simple program consists of several classes and a complex one can have thousand classes.

 

2. What is structure of a class?

Here’s a standard structure of a class in Java:

class NameOfTheClass {
	// fields declaration blocks

	// initialization blocks

	// constructors blocks

	// method blocks
}


All the blocks are optional. You can combine them in any order. For example:

class Dog {
	String name;
	float weight;
	String color;


	{
		color = "Brown";
	}

	Dog(String name) {
		this.name = name;
	}

	void bark() {
		System.out.println("Gow gow!");
	}

	void waveTail() {
		System.out.println("Waving...");
	}
}
That means in a class you should declare fields to represent characteristics and implement methods to represent behaviors of real-world objects.

In the above code you see initialization blocks. Read this article to understand how initialization blocks work in Java.

 

3. What is an object?

An object is an instance of a class. For example, the Dog class is just a blueprint for types of dogs. There is no concrete Dog exists until you create a new object of the Dog class like this:

Dog myDog = new Dog();
Here, the new keyword is used to create a new object of the Dog class. Remember in Java, you always have to use the new keyword to create an instance of a specific class.

Once an object is created, you can use the dot (.) operator to access its fields and methods, for example:

myDog.name = “Rex”;
myDog.bark();
 

4. How many objects can you create?

Almost unlimited. The only limitation is the amount of memory available to store the objects. As you know, a program is a collection of objects that interact with each other, so there will be many objects created during lifetime of a program.

 

5. Where are objects stored?

Objects are stored in a memory location called heap memory.

 

6. Do you need to destroy objects?

NO. It’s because Java has a memory management mechanism called garbage collection, which automatically detects eligible objects which are no longer referenced and destroys them. You don’t have to care about freeing memory manually.

 

7. What is a top-level class?

A top-level class is a class that isn’t nested within another class. For example: String, Object, System, Integer, … are all top-level classes. The Dog class above is also a top-level class.

 

8. Can you mark a class as static?

We can’t mark a top-level class as static, but you can declare a static inner class which is nested within another class. For example:

class Painter {
	static class Palette {
	
	}
}
Here, Palette is a static inner class.

 

9. Which modifiers can you apply for a top-level class?

Only public, package (default), abstract and final. Note that we cannot use both abstract and final together because an abstract class can be extended by subclasses, whereas a final class cannot be extended.

 

10. What is a nested class?

A nested class is any class that is declared within the body of another class or interface. For example:

class Outer {
	class Nested { }
}
You need to have a nested class when it is used internally by the enclosing class, or when the nested class firmly relates to the enclosing class.

 

11. What is an inner class?

An inner class is a nested class that is not static. For example:

class Car {
	class Engine { }
}
Here, Engine is an inner class. So an inner class is a non-static nested class.

 

12. What is an abstract class?

A class is abstract if it is declared with the abstract modifier. For example:

abstract class Human {
}
An abstract class cannot be instantiated (you cannot create new objects of an abstract class).

An abstract class can have both abstract and non-abstract methods. For example:

abstract class Human {
	void eat() {

	}

	abstract void think();
}
You use abstract class to implement default behaviors to be inherited by its subclasses, but no need to create an instance of the abstract class itself.

 

13. What is a final class?

A final class is a class that is declared with the final modifier. For example:

final class BlackHorse {
}
A final class cannot be extended by any classes. For example, String is a final class so you cannot create a subclass of it.

When you don’t want your classes to be extended, use the final modifier.

 

14. How can you create a new instance of an inner class?

To instantiate an inner class, we must have a reference to an instance of the outer class. For example:

class Outer {
	class Inner { }
}
Then we create a new instance of the Inner class like this:

Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
  

15. What is a static nested class?

A static nested class is a class that is nested within another class and is marked with the static modifier. For example:

class OuterOne {
	static class NestedOne {
	}
}
A static nested class is not an inner class. It’s a top-level nested class, because the static nested class doesn’t share any special relationship with an instance of the outer class. Thus we can instantiate a static nested class like this:

OuterOne.NestedOne nested = new OuterOne.NestedOne();
Here, we don’t need an instance of the outer class to create a new object of the static nested class.

Learn more: Java Static Nested Classes and Inner Classes.

 

16. What is a method-local inner class?

A method-local inner class is a class that is declared within a method of the enclosing class. For example:

class Car {
	void start() {
		class Starter extends Thread {
			public void run() {
				System.out.println("Starting...");
			}
		}

		new Starter().start();
	}
}
 

17. What is an anonymous inner class?

An anonymous inner class has no name and is always created as a part of a statement. Its type must be either a subclass of a named type or an implementation of a named interface. Anonymous inner classes are often used with Swing’s event handling. For example:

JButton button = new JButton();

button.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent evt) {

	}
});
Here, we create and pass an anonymous inner class into the addActionListener() method. This anonymous inner class implements the ActionListener interface. Anonymous inner classes are often used for shortening the code.

 

Other Java OOP Tutorials:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#1Ganesh Chittalwar2020-03-29 04:47
wowww what a great blog i love it salute bro realy i m very glad to read this blog and also all problem path clear and all concept understood ..u are really great man ..thanks very much sir
Quote