Core Java - Objects and Class


Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:
  • Class
  • Objects
  • Instance
  • Methods
  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction

  • In this chapter, we will look into the concepts Classes and Objects and methods.
  • Object: Objects have states and behaviors. Eg: A dog has states-color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
  • Class: A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
  • Methods: A method is basically a behavior. A class can contain many methods.There will be many methods where the logics are written, data is manipulated and all the actions are executed.

  • Objects in Java:

    Let'd discuss what are objects.For example we can consider the real-world where we can find many objects, Dogs, Humans, Bikes, Cars etc. All these objects have a state and behavior. If we consider a Bike, then its state is - name, company, color,price and the behavior is - starting, running,stopping.

    Classes in Java:

    A class is a template/blue print from which individual objects are created. A sample of a class is given below:
    Eg:
        public class Bike{
                //states
                String name;
                String company;
                String color;
                double price;
    
                //behaviours
                void starting(){
                }
                void running(){
                }
                void stopping(){
                }
            }
    
    A class can contains the following variable types of
  • 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.
  • Class variables: Class variables are variables declared within a class(or)outside any method.
  • A class can have any number of methods to access the value of various kinds of methods.

    Constructors:

    When discussing about classes, one of the most important subtopic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class the Java compiler will defaultly build a constructor called as default constructor for that class.
    Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.
    Similarly, like methods we have two types of constructors are
    1)No-argument constructor
    2)Parameterized constructor
    Note:It will not have modifier and return type.
    Eg:
        public class Songs{
            public Songs(){
                //This constructor is called as Default constructor.
            }
            public Songs(String name){
                // This constructor is called as Parameterized constructor.
            }
        }
    

    Creating an Object:

    As we mentioned before, class provides the blueprint for objects. So normally an object is created from a class. In Java the new keyword is used to create new objects. There are three steps for creating an object from a class:
  • Declaration: A variable declaration with a variable name with an object type.
  • Instantiation: The 'new' keyword is used to create the object.
  • Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
  • Eg:
        public class Songs{
            public Songs(String name){
                // This constructor is called as Parameterized constructor.
                System.out.println("Genre of the song is :"+ name );
            }
            public static void main(String[]args){
                // Following statement would create an object myPuppy
                Songs ref =new Songs("melody songs...");
            }
        }
    
    Output:
    Genre of the song is :melody songs...

    Accessing Instance Variables and Methods:

    Instance variables and methods are accessed via created objects. To access an instance variable the fully qualified path should be as follows:
        /* Creating object */
            ObjectReference = new Constructor();
        /* Calling a variable */
            ObjectReference.variableName;
        /* Calling a class method */
            ObjectReference.MethodName();
    
    Eg:
        public class Songs{
            String name;
        
            public Songs(String name){
                    System.out.println("Genre of the song is : "+ name );
            }
            public void setname(String name ){
                    this.name = name;
                }            
            public String getname(){
                    System.out.println("Year of song is : "+ name );
                return name;
                }            
            public static void main(String[]args){
                    Songs ref =new Songs("melody songs...");
                /*Calling a class method to set genre of song */
                    ref.setname("1990's");
                /* Calling a class method to get genre of song */
                    ref.getname();
                /* You can access instance variable as follows as well */
                    System.out.println("Variable Value : "+ ref.name );
            }
        }
    
    Output:
    Genre of the song is : melody songs...
    Year of song is : 1990's
    Variable Value : 1990's

    Java Package:

    In simple, it is a way of categorizing the classes and interfaces. When developing applications in Java, hundreds of classes and interfaces will be written, therefore categorizing these classes is a must as well as makes life much easier.
    For further details of the package http://blogs.tesdbacademy.com/java-package.php

    (Core Java - Datatypes)