Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Describing Object Class in Java

In Object Oriented Programming, classes and objects are normally used, in this article we will learn how to use it in Java.

Classes and Objects in Java Programming:

OOP is called Object Oriented Programming. So in this case we are dealing with classes and objects. In this tutorial we will discuss the class and object in detail. As we learnt in last tutorial about the modifiers in Java and there are two types of modifiers: Non-Access modifiers and Access Modifiers. So we will use some of the Non-Access Modifiers in this tutorial.

Listing 1: Creating Simple Class for Date

importjava.util.Date;

public class DisplayDate
{
public static void main(String args[]){
    Date date = new Date();
System.out.println(date.toString());
  }
}

As you can see in listing 1, I created a simple class and named it DisplayDate. And I import the Date and used an object of Date from which we got the current date and time. So by using the class we can deal with the data just using the class name and create an object of the class. We will study more about them in this tutorial lets we study the abstract class.

Abstract Class:

When we use the abstract keyword with class, so we can’t create any object of that abstract class. We should override the class to use its data. There is a good example about the abstract class in listing 2. Check the listings to understand the feature of abstract class. As you know that we use abstract to hide the data of the class. As there is a best example for the abstraction, now a days everybody is using mobiles. We commonly send messages to any number, when we typed the number then we type the message and in last we press the send or OK button. Then message transfers to the desired number. But we don’t know what is happening behind the scene. Exactly like this, it happens in abstract class. We don’t know what is happening and why. We can say that in this case our data is secure during the execution.

Listing 2: Abstract Class “CurrentDate.java”

abstract class CurrentDate{
publicint day = 10;
publicint month = 9;
publicint year = 2013;
public void DisplayCurrentDate(){
  }
}

Listing 3: Abstract Class “CurrentTime.java”

public class CurrentTime extends CurrentDate{
publicint minute = 53;
publicint second = 29;
publicint hour = 12;
public void DisplayCurrentDate()
  {
System.out.println("The Manual Entered Date is: "+day+"/"+month+"/"+year);
  }
public void DisplayCurrentTime()
  {
System.out.println("The Mannual Entered Time is: "+hour+":"+minute+":"+second);
  }
public static void main(String args[]){
CurrentTime time = new CurrentTime();
time.DisplayCurrentDate();
time.DisplayCurrentTime();
  }
}

As you can see the both listings 2 & 3, there are two classes. The class in listing 2 is abstract and we declared three variables as public. Its mean that the public can be execute anywhere in the package. And there is also a public method in abstract class. According to the abstract class definition the data is hide and nobody knows that, what they are seeing how it happens. So the data is fully secure during the execution.

Objects in Java Programming:

As you can object is declared and also had been used in this tutorial, using the above three listings 1, 2 & 3. In these programs I wrote simple programs in java program which you can understand. Now I will clear you more about the object. When we declare any object of a class, so it is important to use the keyword new as there is an example: CAR car1 = new CAR();

In this case the capital letter car is class and the car1 is the object and we used keyword new to call the constructor. But if you don’t create any constructor in the class we use the same thing as it is. We will study the constructors in this tutorial later. But we should more understand the objects.

Sometimes we need to declare the variables using the primitive type as there are about to 8 primitive types. If we look deep about the primitive types so they could also be called class, because we don’t know which type of code is done inside the INT, DOUBLE, FLOAT etc. what we do, just use the int and declare any name as a variable. Same like that we use name of the class and then we declare any variable as object. And then we use the object to call the internal methods of the class. Check the listing 4 for more understand but the class is without any constructor.

Listing 4: Objects in Java Programming

public class Library{
intTotalBooks = 35902;;
publicintShelfNumber = 2352;
public String NameOfLibrary = "Place of Education" ;
public void LibraryRecords(){
System.out.printf("\nThe name of the Library is: %s...",NameOfLibrary);
System.out.println("Welcome To Engineering Section...");
System.out.printf("There are %d number of books in the Engineering Library...\n",TotalBooks);
System.out.printf("\nThere are %d number of Shelfs for books...",ShelfNumber);
  }
public static void main(String args[]){
    Library lib = new Library();
lib.LibraryRecords();
  }
}

As you can see in the listing 4, there is a class name library and there is one method inside the class. But there is no any constructor. But we have declared an object for it, and then by the help of the object we called the internal method LibraryRecord using the object.

As mentioned before we don’t know what is going on in the class but we have information about the class for which purpose we can use it. And in this simple class of the listing 4, we know there is one method and we will get information about the library. But when we check the object so we don’t know how it will send information to use.

NOTE: Class and Objects are used to define the full details about anything. If I talk about the animals so there is a class about the animals and that class will contain many types of animals? Because there is no any single animal in the world. And when we will use the class animal so there will be subclasses of the class animal. For example there is class animal and in class we used the dog method so we will create another class for the dogs because there are different verity dogs. For this purpose we used to declare a subclass to define the details of the class. For more details about the class and subclass check the figure 1.

Class and Subclass

Figure 1: Class and Subclass

Constructor:

The constructor are of two types. Non argument constructor and argument constructor. It looks same like the methods but there is one thing different from the methods. We use the same name for the constructors by which we declared the class. And during the declaration of the objects, we can send data to the constructor as I told you that there is argument and non-argument constructors. For more details check the listing 5.

Listing 5: Constructors in Java Programming

public class Animal{
public Animal()
  {
System.out.println("Non-Argument Constructor is called...");
  }
public Animal(String name)
  {
System.out.println("You Entered "+name+" as animal name...");
  }
public static void main(String args[]){
    Animal animal1 = new Animal();  //calling the non-argument constructor
    Animal animal2 = new Animal("Cow"); //calling the argument constructor and sending cow as argument
  }
}

As you can see in the listing 5, there is a class name is Animal and there two constructors as I told you there are two types of constructors argument and non-argument constructors. Same like this in the Animal class. And I declared two objects animal1 and animal2, and then using the keyword new to call the both constructors separately.

In this tutorial we learnt about the classes and objects in details and you can see their examples for your understand. We use classes and objects for same purpose as we used them in C++. And also we discussed the constructors.

We learnt about the constructors to make you understand how you can use the constructors in your programming. So keep it up and practice them try to create different classes with constructors and use objects to execute the data from the classes.

See also



Java and Android application developer with more than 3 years experience.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login