Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

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.

Article: Java classes and objects
Quizzes: Beginner  Intermediate  Advanced 

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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.



Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

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! If you need more explanation by any of the answers, just let me know to update it for you!

Megisoft.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. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Article: Java classes and objects
Quizzes: Beginner  Intermediate  Advanced 

Suggested Articles:


Click Here For Info about: Sar Maroof's New Java Book
Author: Sar Maroof
More than a decade of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster - Mind IT
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 Subscribe To Megisoft RSS Feeds! (Your Browser)
 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

Leave a Reply

megisoft © 2016