Java classes and objects

What is a class?

Java code contain either classes or interfaces. Classes are a kind of template which can be used to create unlimited objects of its type. Each class contains variables and methods.

 

What is an object?

If a class is template, then an object is the implementation of that template. classes contain logical information while objects represent the physical info. For example a class “Subscriber” contains “name, gender, age, education“. By instantiating an object from that class, we can have the real info about each subscriber like “Jack, male, 25 year, college“. The class determines which information you can have about each object. In our example we can’t have information about Jack’s height, because the class doesn’t contain the attribute height. In case you want to know that info, then you need to add it to the class first.

classes-and-objects-color

How to instantiate objects from a Java class?

We can create the class Subscriber in Java as shown below. To instantiate objects from the class Subscriber we use for example;

Subscriber subscriber = new Subscriber();
Subscriber subscriber2 = new Subscriber();

Keep in mind that the object is created when you use “= new Subscriber();” , but the statement

Subscriber sub; created only the reference sub to the object.

Example:

Here below is the example how we create an object from a class.

If we execute the main code, it will write the following to the standard output.

Name: Jack
Gender: male
age: 25
education: college

public class Subscriber {

	// variables
	String name;
	String gender;
	int age;
	String education;
	// The statements in the main method are executed
	public static void main(String[] args){
		// instantiating a subscriber object "sub" from the class Subscriber
		Subscriber sub = new Subscriber();
		sub.name = "Jack";
		sub.gender = "male";
		sub.age = 25;
		sub.education = "college";

		System.out.println("-" + sub.name);
		System.out.println("-" + sub.gender);
		System.out.println("-" + sub.age);
		System.out.println("-" + sub.education);
	}
}

 

Exercise 1

What is written to the standard output as the result of executing the following code?

class Laptop {

	String brand;
	int screenSize;
	double price;

	public static void main(String[] args){
		Laptop lap = new Laptop();
		Laptop lap2 = new Laptop();
		lap.brand = "Acer";
		lap.screenSize = 17;
		lap.price = 377.0;
		lap2.brand = "Dell";
		lap2.screenSize = 15;
		lap2.price = 300.0;

		System.out.print("" + lap.brand);
		System.out.print("-" + lap2.screenSize);
		System.out.print("-" + lap.price);
	}
}

Select the correct answer.


Exercise 2

What is written to the standard output as the result of executing the following code?

class Laptop {

	String brand;
	int screenSize;
	double price;

	public static void main(String[] args){
		Laptop lap = new Laptop();
		Laptop lap2 = new Laptop();
		lap.screenSize = 17;
		lap2.brand = "Dell";
		lap2.price = 300.0;

		System.out.print("" + lap.brand);
		System.out.print("-" + lap2.screenSize);
		System.out.print("-" + lap2.price);
	}
}

Select the correct answer.


Exercise 3

1-) What is written to the standard output as the result of executing the following code?

2-) what happens if we change the last line from System.out.print(“-“+lap2); to System.out.print(“-“+lap);

class Laptop {

	String brand;
	int screenSize;
	double price;

	public static void main(String[] args){
		Laptop lap;
		Laptop lap2 = new Laptop();
		lap2.brand = "Dell";
		lap2.price = 300.0;
		lap2.screenSize = 15;

		System.out.print("-" + lap2);
	}
}

Select the correct answer.


Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.


Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

5 Comments

Add a Comment
  1. You mean I don’t have to pay for expert advice like this anymore!

    1. No my friend, it is completely free:)

  2. Thank you so much for posting this content!

    1. It’s a real plseaure to find someone who can think like that

    2. You are welcome!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017