Powered by Blogger.

Learn Java | Classes and Objects in Java

Definition of Object Class:-

In the Java technology, the Object class is the root of all classes. If a class in Java programming does not extends any class then this class extends the Object class by default.

Exaple of  Object Class:-

 public class Car{

 }

 // or we can declare the Car class by

 // extending Object class. Both have same mining.

 public class Car extends Object{

 }

Java is an Object Oriented Language. As a language that has the Object Oriented feature Java supports the following fundamental concepts:

    # Polymorphism
    # Inheritance
    # Encapsulation
    # Abstraction
    # Classes
    # Objects
    # Instance
    # Method
    # Message Parsing

In this chapter we will look into the concepts Classes and Objects.

1. Object - Objects have states and behaviors. Example: A dog has states-color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.

2. Class - A class can be defined as a template/ blue print that describe the behaviors/states that object of its type support.

Objects in Java:-

Let us now look deep into what are objects. If we consider the real-world we can find many objects around us, Cars, Dogs, Humans etc. All these objects have a state and behavior.

If we consider a dog then its state is . name, breed, color, and the behavior is . barking, wagging, running

If you compare the software object with a real world object, they have very similar characteristics.

Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.

So in software development methods operate on the internal state of an object and the object-to-object communication is done via methods.

Classes in Java:

A class is a blue print from which individual objects are created.

A sample of a class is given below:

public class Dog{
   String breed;
   int age;
   String color;

   void barking(){
   }
  
   void hungry(){
   }
  
   void sleeping(){
   }
}

A class can contain any of the following variable types.

1. Local variables:- variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

2. Instance variables:- Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

3. Class variables:- Class variables are variables declared with in a class, outside any method, with the static keyword.

A class can have any number of methods to access the value of various kind of methods. In the above example, barking(), hungry() and sleeping() are variables.

Below mentioned are some of the important topics that need to be discussed when looking into classes of the Java Language.

Creating an Object:

As mentioned previously a class provides the blueprints for objects. So basically an object is created from a class. In java the new key word is used to create new objects.

There are three steps when creating an object from a class:

1. Declaration:- A variable declaration with a variable name with an object type.

2. Instantiation:- The 'new' key word is used to create the object.

3. Initialization:- The 'new' keyword is followed by a call o a constructor. This call initializes the new object.

Example of Creating an Object is Given Below:

class Puppy{
   public Puppy(String name){
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }
   public static void main(String []args){
      // Following statement would create an object myPuppy
      Puppy myPuppy = new Puppy( "tommy" );
   }
}

If we compile and run the above program then it would produce following result:

Passed Name is: tommy

The Object Class:-

Object is the base class for extensible classes in the Recess. Object introduces a standard for building a class descriptor through reflection and the realization of Recess Annotations. Object also introduces the ability to attach methods to a class at run-time and create wrappable methods.

Object is the superclass for the major components of Recess: Models, Controllers, and Views. The two most common types of classes created when using the Recess Framework are Models and Controllers. There is nothing prohibiting developers from extending Object in their own custom classes. In fact, if you are a developer feeling particularly ambitious and needing to roll a mini-framework with a different composition than Recess, starting with Object and other primitives like Library defined in Recess Core may make your job much more pleasant!

A primary purpose of any framework is to remove boilerplate code whereever possible. Object-oriented (OO) languages provide different means for removing code-duplication. The most commonly used techniques in well-known OO languages of Java and C++ are inheritance and composition. PHP has support for these common techniques and Recess makes use of them wherever possible. Other languages that support object-oriented programming like SmallTalk, Scala, and Ruby go beyond composition and inheritance and allow modules or traits to attach new units of functionality to class definitions (like properties and methods) dynamically. Recess Core provides a systematic way for simulating similar language features and allows for methods to be attached to classes that extend Object at run-time. This technique in combination with annotations is how the Recess ORM provides methods to access relationships, for example. Further discussion of attached methods see the section called Attached Methods.

Another form of extensibility in Recess that draws inspiration from a more sophisticated Aspect-Oriented Programming and the 'similar in spirit' Decorator OO pattern are wrappable methods. Classes extending Object can declare a method to be !Wrappable. Once wrappable, other classes implementing the IWrapper interface can dynamically wrap functionality around calls to the method. Key methods in the framework, like Controller's serve and Model's insert, update, delete, are wrappable. Why is this useful? It allows user-defined functionality to inject behavior just before or just after core framework method calls in a simple, standard way. For example, validations on a Model may take place just before insert or update, using wrappable methods the validation system does not need to be closely coupled to the Model class. Similarly with Controllers, authentication could happen before serve is called and the wrapping authentication class can subvert a request by redirecting to a login page. Further discussion on wrappable methods see the section called “!Wrappable Methods and the IWrapper Interface.

The final major unit of functionality exposed in Object is a system for realizing Recess Annotations. Annotations give programmers the ability to work in a more declarative, meta-programming style. What does this mean? Rather than telling PHP how to do something, you tell Recess what you want with an annotation and it is then up to Recess and the annotation's class to expand your declarative statement into PHP. Annotations can be written on three PHP constructs: classes, methods, and properties. Because PHP does not have first-class support for annotations, Recess Annotations are placed inside of doccomments, comments that begin with /**, which are a first-class construct in PHP available through PHP's Reflection API. Annotations often make use of attached and wrappable methods to inject units of functionality. For further discussion on annotations see the section called Annotations.

Underlying attached methods, wrappable methods, and annotations is fundamental data structure, the ClassDescriptor. The ClassDescriptor is where the information used to describe Recess' language features like attached methods are stored. There is one ClassDescriptor per class and, when Recess is running in production mode, this data-structure is computed once and cached. Sub-classes of Object can use the class descriptor as a store for computed data structures by overriding hooks in the Object class. For example, Model uses this cache to hold database information and the meta-data for relationships, and Controller uses it to store routing information. Annotations expand to 'shape' a class's ClassDescriptor. The following section describes the hooks available to sub-classes of Object for shaping class descriptors.

2 comments:

  1. I appreciate you sharing this article. Really thank you! Much obliged.
    This is one awesome blog article. Much thanks again.

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete
  2. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete