Sunday, October 13, 2013

Classes, methods and objects in Java

Classes in Java

  • Anything that you want to represent in Java, should be capsuled to a class.
  • A class is a blue print from which individual objects are created.

  • Class create objects and methods are used to communicate between these objects.
  • Actually methods are behaviors of objects. 



How to declare a class 
  • Simply a class can be declared as follows.

class ClassName{
   variables;
   methods();
}

  • This Java naming conventions post may help you to understand; how to declare a class with correct Java standards.
  • According to the object's behaviors, you can modify a class with Access modifiers, Non access modifiers, implementing to an interface or extending to a class.

public class ClassName extends AnotherClass implements interface1, interface2{
   variables;
   methods();
}


  • In Java you can inherit from only one class but more interfaces. 
  • Access or non access modifiers should be used just before the class keyword. 


    Methods in Java


    • Methods are used to manipulate data in an object.
    • Actually methods are behaviors of an object (class). Look at the following example.

    accessModifier returnType methodName(parameter list){
       method body
    }
    
    

    Access modifiers
    • Access modifiers gives permissions to access. As you can see access modifiers can be used for classes, methods and also variables.
    • You may look the following example about Access modifiers.
    Return type
    • Return types are used to represent the type of returning element.
    • It can be int, double, char, String, void, Object or any type.
    • If you use void, it means you don't return anything.
    • Look at this post which is about Java naming standards. 
    Parameter list
    • Parameters should be used with in brackets in a method.
    • Look at the following example.

    public int getData(int x, int y){
      
    }
    
    

    Types of methods

    Basically there are three types of methods.
    1. Instance methods
    2. Static methods
    3. Abstract methods
    Instance methods
    • These methods are loading when object is created.
    • We can call instance methods just calling through objects.
    public int getData(int x, int y){
      
    }
    

    Static methods
    • Static methods are defined, using "static" keyword.
    • We need not to use objects to call static methods, so we can call it by using the class name. But if you want it can be called through objects.


      public class Demo {
      
         public static void main(String[] args) {
             eat(); //Just calling method
             Demo.eat(); // Calling method through class
        
             Demo demo = new Demo();
             demo.eat(); //calling methof through class object
         }
       
         public static void eat(){
             System.out.println("eat method");
         }
      }
      
      

      Abstract methods
      • These methods are defined with abstract keyword before method name.
      • Abstract methods are defined without an implementation.
      • It means there is no method body in abstract classes.
      • Semi colon is used instead of curly braces. Look at the following example.
      public abstract int getData();
      

      Method argument passing
      1. Object type
      2. Primitive type
      Object type
      • Original value can be changed within this type.
      Primitive type
      • In this type we just using a copy of the original variable.
      • The value can be changed inside method, but no effect to original value.

      public class Demo {
         int i;
       
         public static void main(String args[]) {
             Demo demo = new Demo();
             demo.i = 10;
      
             System.out.println(demo.i);
             demo.CheckResult(demo);
             System.out.println(demo.i);
         }
      
         public void CheckResult(Demo d) {
             i = 50;
             System.out.println(i);
         }
      }
      
      

      Objects in Java


      • Objects are real world objects (Car, Dog, Student...etc.)
      • Object has states and behaviors.
      • Just a moment think about a cat.
                         states - color, name, breed
                         behaviors - eat(), drink(), walk()


      • Scientifically Java Objects are defined as a memory locations in RAM.


      How to create objects ?



      Look at the following example to get an idea about objects.

      public class Demo{
          public static void main(String[] args) {
              Demo obj1 = new Demo();
              Demo obj2 = new Demo();
              obj1.apple();
              obj2.mango();
          }
      
          public void apple() {
              System.out.println("This is apple method");
          }
      
          public void mango() {
              System.out.println("This is mango method");
          }
      }