Thursday, 10 May 2018

Chaudhari Karan

Java - Objects and Classes

    No comments:



Lecture 3  Java – Objects and Classes


Hello friends, today we will discuss chapter Objects and Classes.

Objects and Class in java

Java has some object-oriented feature and some fundamental concepts:
  • ·         Abstraction
  • ·         Classes
  • ·         Encapsulation
  • ·         Inheritance
  • ·         Instance
  • ·         Method
  • ·         Massage Parsing
  • ·         Objects
  • ·         Polymorphism

In this chapter, we will discuss Objects and Classes concepts.

Objects: In java object is states and behavior. Example: A cat has states – color, name, etc. and behavior – eating, walking, etc.

Classes: Class describes the state and behavior of objects.

Objects in Java :

Now, we look objects in deep if we look around us, we find many objects which have states and behavior like cars, humans, dogs, etc.

If we consider the car as an object so the car has state – color, name, etc. and its behavior – speed, running, etc.

If we compare objects with software, so find the very similarity.

If we consider software as an object, so it has states and behavior. Like in software object state is stored files and behavior is methods.

How to create an object in Java :

In java object is created from the class. For create objects new keyword used in java.

Here is an example of object :

Test t = new Test();  
Hello h = new Hello();

In the 1st  example, I want to create Test class object. So used the Test as variable name and t is reference variable. After that used the new keyword for the object and variable name. 

Here is practical of objects creating :

Object

Output: Passed name is: Tommy

Classes in Java :

The class is state and behavior of Object.

Example of the class:

The class contains any type of variable.

Class


Local variable: Variable is defined inside constructor and methods are known as the local variable. A variable is declared and initialized in method and it destroys after completed.

Class variable: Variable is declared with the name outside the method, with any static keyword is called the Class variable :

Instance variable: Variable with a class but it is outside the method. These variable accesses from inside any constructor and method.

The class has the number of methods to access the various type of methods. Example, sleeping(), running(), eating().

Constructor: 


We discussing class. The class subtopic is the constructor. Every class has a constructor. In Java, if we do not create explicitly constructor for a class, so java compiler creates the default constructor for that class.

Every time created the new object, at least one constructor will be invoked. The main rule of the constructor is they should have the same name of the class. A class has more than one constructor.

Example of a constructor :

Constructor

Java supports Singleton Classes. Where able to create to only one instance class.

How to use Singleton Class:


Singleton Classes purpose is to control object creation and limiting the number of objects to only one. There has only one Singleton instance class, any instance field occurs only once per class, like the static field.

For example of Singleton Class, If you have only one connection to your database has trouble from multithreading, the Singleton makes only one connection or thread which can access connection at a time.


Implementing Singleton:


Example 1

Singleton Class

Output: demoMethod for singleton

Example 2

Following program shows the implementation of classic singleton design pattern.

Singleton class

ClassicSingleton class maintain a static reference to the long singleton instance and return the reference from static getInstance() method.

ClassicSingleton class employs a technique which called as instantiation to create Singleton class. Singleton instance is not created until getInstance() method. 

Accessing Instance Variable and Methods:


An instance variable is accessed by the creating the object. To access instance variable following is fully qualified path :

Instance variable


Example:

In this example explain how to access instance variable and method of the class.

Instance variable

Output:  

Name chosen is: Tommy
Puppy's age is: 2
Variable Value: 2

Source file declaration rules :

Here we discuss some declaration rules. It is essential in classes, import statement, and package in the source file.

  • ·         There is only one public class in one source file.
  • ·         Source file has multiple non-public classes.
  • ·         The public class name is source file name which is appended with .java Example: public class name Puppy so source file name is Puppy.java
  • ·         If the class is defined inside the package, then package statement first statement in the source file.
  • ·         If import statement is present in the file, then they were written between the package statement and class declaration. If there is not present the package statement then import statement is the first line in the source file.
  • ·         Package and Import statements will imply to all present classes in the source file. It is not possible to declare different package and import statements in different classes in the source file.

Package in Java :


Java package is a categorizing the interfaces and classes. When developing an application in java there is package has hundreds of classes and interfaces which it makes much easier.

Import in Java :


In Java, Import statement is used for Import some file and classes.
Example: import java.io.*;

Next: In next lecture, we will discuss basic data types in Java.

Subscribe to this Blog via Email :