Class & Object

Class & Object

Class & Object in Java

Written by: saeed1907057

Sun, 17 Nov 2024

class

  • Basic unit of Object-Oriented Programming paradigm(pattern),
  • Ex: Animal is a class and tiger is an object of Animal class,

Structure of JAVA class

<<modifiers>> class <<class name>> {
    // Body of the class goes here
}
  • Modifiers:
    • Keywords that associate special meanings to the class declaration,
    • May have zero or more modifiers,
  • Class name:
    • User defined class name,
    • Follows Pascal Case naming convention,
  • Body of the class:
    • Contains fields, methods,
  • Ex: (Hooman.java)
    public class Hooman {
      ...
    }
    
    • has public modifier,
    • class name is Hooman,
    • body contains field,constructor & some methods,

Components of java class

  • A class in Java may consist of 5 components:

  • Field / variables

    • Represent properties or attributes of objects of that class,
    • 2 types of fields for a class:
      • Instance variables:

        • Non-static variables,
        • Every object contains separate copy of these variables,
        • Must be accessed using object of the class,
        • Can be accessed using dot(.),
        • Ex: obj.name;,
      • Class variables:

        • Static variables,
        • Declared using the static keyword as a modifier,
        • Every object uses same copy of these variables,
        • Shared among all objects of this class,
        • Can be called using class name,
        • Can be accessed using dot(.) on both object and class,
        • Ex: MyClass.count,
    • static as well as non-static fields are initialized to a default value,
      • A numeric field (int, long, ...) is initialized to zero(0),
      • A boolean field is initialized to false,
      • A reference type field is initialized to null,
  • Method

    • Represent behavior of objects of the class,
    • More info is discussed below,
  • Constructor

    • Used to create objects of the class,
    • More info is discussed below,
  • Static initializer

    • For initializing static variables which need some calculation,
  • Instance initializer,

    • For initializing variables which need some calculation,

Understanding components

  • this refers to current instance. More details is discussed next section,
  • See Hooman.java,
    public class Hooman {
        // Field - 2 Class variables
        public static final String SPECIES;
        private static int objectCounter = 0;
    
        // Field - 3 instance variables
        private String name;
        private int age;
        private boolean isAdult;
    
        //static initializers - 1
        static {
            // normally more calculation is performed as per need
            SPECIES = "Homo Sapiens";
        }
    
        //instance initializers - 1
        {
            isAdult = false; // by default false though
        }
    
    
        //constructor - 1
        public Hooman() {
            objectCounter++;
        }
        //constructor - 2
        public Hooman(String name, int age) {
            this.name = name;
            this.age = age;
            isAdult = age >= 18;
            objectCounter++;
        }
    
        // methods - 6 methods
        public String getName() {...}
        public void setName(String name) {...}
        public int getAge() {...}
        ...
    }
    

Method again

  • Represent behavior of objects of the class,
  • Is a named block of code,
  • Structure:
    <<modifiers>> <<return type>> <<method name>> (<<parameters list>>) <<throws clause>> {
      // Body of the method goes here
    }
    
  • Formal parameters of a method are treated as local variables,
  • throws is optional,
  • Ex:
    public void setAge(int age) {
       if(age < 0 || age < this.age) return;
       this.age = age;
       isAdult = (age>=18);
    }
    
  • return-type can be any data type,
  • void means doesn't return any value,
  • Follows camelCase naming convention,
  • Method signature:
    • Combination of
      • method name, and
      • its parameters
        • number,
        • types,
        • order,
    • Modifiers, return-type, and parameter names aren't part of the signature,
    • Ex:
       public int add(int num1, int num2){
          return num1+num2;
       }
      
      • Signature is: add(int, int);
  • Not allowed to have more than one method in a class with the same signature,
  • A class can have two types of method like variables:
    • Instant method and
    • Class method,

Instance Method

  • Non-static method,
  • Used to implement behavior of the objects of the class,
  • Must be called using object,
  • Ex: See showSpecificCharacter() in Hooman.java,
    public void showSpecificCharacter(){
        System.out.println("Name is => "+name);
    }
    
    Accessing like this
    Hooman anik = new Hooman("Anik",22);
    anik.showSpecificCharacter(); // Name is => Anik
    

Class Method

  • static method,
  • Used to implement the behavior of the class itself,
  • Can be called using both object or Class name(Recommended) itself,
  • Can't access instant variables here, guess why?
  • Ex: See showSomeCharacter() in Hooman.java,
    public static void showSomeCharacter(){
      System.out.println("General characteristics");
      //System.out.println(name);//can't access instant variable here
    }
    
    Calling like this
    Hooman anik = new Hooman("Anik",22);
    anik.showSomeCharacter(); // General characteristics -------(a)
    Hooman.showSomeCharacter(); // General characteristics -----(b)
    
    • (a) is ok but not recommended,
    • (b) best practice,

Special main() method

  • Special method having public,static, void, method name main and having a String array as parameter,
    public static void main(String[] args) {
      // entry point of execution
    }
    
  • JVM invokes this method. If not found, then JVM does not know where to start the application. So, it throws an error stating that no main() method was found,
  • You can have as many as main() method in a class having different signature. But JVM will call only the above one,
  • You don't need to write main method in a class if you don't want it to start from here,
  • You can also call main() method just like other method,
  • Ex: See Test.java,
    public class Test {
        public static void main(String[] args) {
           ...
        }
        ...
    }
    

Invoking/Calling method

  • Instance method is invoked on an instance of the class using dot(.) notation,
  • Class method is invoked on Class name using dot(.) notation,
  • Ex: See main() in Test.java,
    Hooman anik = new Hooman("Anik",22);
    anik.showSpecificCharacter(); // instance
    
    Hooman.showSomeCharacter(); // static
    

Constructor

  • Used to initialize an object of a class,
  • Constructor name is the same as the simple name of the class,
  • Doesn't have return type,
  • Basic Structure(can have throws clause also):
    <<Modifiers>> <<Constructor Name>>(<<parameters list>>){
     // Body of constructor goes here
    }
    
  • Overloading of constructor is possible,
  • If a class has multiple constructors, all of them must differ from the others in the number, order, or type of parameters,
  • One constructor can call other constructor,
  • Access modifiers are same as others,
  • By default, JVM adds a default constructor as long as there is no constructor.
  • default constructor doesn't accept any parameter and does nothing,
  • default constructor only satisfies logic so that we can create object,
  • You can't declare a constructor static. Remember static is part of class not object,
  • Ex: See Bird.java,
    public class Bird {
        ...
        // constructor - 1
        public Bird() {
            this.name = "Unknown";
            this.species = "Not found";
            this.canFly = false;
        }
    
        // constructor - 2
        public Bird(boolean canFly) {
            this("unknown","Not found",canFly); // calling other constructor. Remember `this`. must be first line
        }
    
        // constructor - 3
        public Bird(String name, String species, boolean canFly) {
            this.name = name;
            this.species = species;
            this.canFly = canFly;
        }
    
        // constructor - 4 . Copy constructor
        public Bird(Bird bird){
            this.name = bird.name;
            this.species = bird.species;
            this.canFly = bird.canFly;
        }
        ...
    }
    
    Creating object like this: See birdTest() in Test.java:
    private static void birdTest(){
        Bird deadBird = new Bird(); // 1
        deadBird.printDetails(); // Unknown -> Not found -> false
    
        Bird unknownBird = new Bird(true); // 2
        unknownBird.printDetails(); // unknown -> Not found -> true
    
        Bird eagle = new Bird("Eagle", "Eagle", true); // 3
        eagle.printDetails(); // Eagle -> Eagle -> true
    
        Bird secondEagle = new Bird(eagle); // 4
        secondEagle.printDetails(); // Eagle -> Eagle -> true
    }
    

Creating instance of a class

  • Object can be created by calling its constructor.
  • Ex: new Hooman();, This object will be automatically deleted, since we are not assigning it in any variable.
  • When you create an object using new keyword, it allocates memory for each instance field of the class,
  • Java runtime takes care of memory allocation, you don't need it,
  • Creating object and calling method:
    Hooman saeed = new Hooman("Saeed",21);
    System.out.println(saeed.isAdult()); // true
    

Local variable

  • Variable declared inside method, constructor, block,
  • Formal parameters for a method are treated as local variables,
  • Not initialized by default,
  • Can't be used until it is assigned a value. Will show error instead of garbage value,
  • Must be declared before it is used,
  • If variables having same name found, local variable get precedence,
  • See LocalVariable.java for full code,
    public class LocalVariable {
        ...
        private int num; // global, value = 0
        ...
    
        public void assignMust(){
            int num; // local, no default value
            //System.out.println(num); // error. Not initialized.
        }
    
        public int calculateDifference(int num2){
            int num = 1001; // local
            System.out.println(num); // 1001 since local get precedence
            return this.num - num2; // this.num refers to global num
        }
    }
    

Null

  • A special reference type called null type,
  • It has no name,
  • Assignment compatible with any other reference type. Ex:
    String name = null; // ok
    Integer number = null; // ok
    Hooman none = null; // ok
    int count = null; // error. int is not reference type. Remember?
    
  • After making sh = null;, the object sh was referring will be destroyed by JVM, since it is no longer accessible.
    Hooman sh = new Hooman("sh",22);
    sh = null;
    

this

  • Extremely useful keyword,
  • Reference to the current instance of a class. (Alert it's for instance, not class),
  • Can be used only in the context of an instance/object,
  • Ex: See TriState.java,
    public class TriState {
        ...
        private StateEnum state = StateEnum.FIRST; // will be discussed later
        public TriState(StateEnum state) { // state = SECOND is passed
            System.out.println(this.state); // FIRST
            System.out.println(state); // SECOND
            this.state = state; // this.state refers to the global state --- (a)
        }
        ...
    }
    
  • Creating instance of above class
    TriState triState = new TriState(SECOND);
    System.out.println(triState.state); ---- (b)
    
  • triState.state in outside is same as this.state inside the class for a specific object of the class. See (b) and (a).
  • Practice yourself,

final

  • prevent modification on which it is used,
  • For declaring constant,
  • Can be used in the following 3 contexts:
    • Variable
      • Can be assigned a value only once,
      • Can't reassign after assigning,
      • Can be assigned while declaration or later,
      • If you do not initialize a final variable at the time of its declaration, such a variable is known as a blank final variable,
      • Ex:
        final int total;
        
    • Class:
      • final class can't be extended or inherited,
    • Method:
      • final method can't be redefined (overridden or hidden) in the subclasses,

final local variable && final parameter

  • Ex: See getArea() of Test.java:
    private static double getArea(final double r){ // final parameter
       final double pi = 3.1415; // final local variable
       //pi = 3; // error
       //r = 5; // error
       return pi*r*r;
    }
    

final instance variable

  • Can be final or blank final,
  • Rules apply for initializing a blank final instance variable:
    • Must be initialized in one of the instance initializers or in all constructors,
    • Don't be confused. Just remember that you can only assign value at once. If you try to reassign it, you will get error,
  • All blank final variables and final reference variables are runtime constants. i.e. they are calculated at runtime,
  • Ex: See Circle.java,
    public final class Circle {
    
        public static final double PI = 3.14159; // final
    
        private final double r; // blank - final
    
        public Circle() {
            this.r = 0;
        }
    
        public Circle(double r) {
            this.r = r;
        }
    
        public Circle(Circle c){
            this(c.r); // it will assign value
            //this.r = c.r; // error, since already assigned in previous line
        }
    
        public double getArea(){
            return PI * r * r;
        }
    
    }
      
    

final class variable

  • Same like previous,
  • Good practice to use capital letter for variable naming,
  • Ex:
    public static final double PI = 3.14159;
    

Varargs

  • Full form is variable-length arguments,
  • Can be used both in method and constructor,
  • 3dots(...) is used,
  • We can pass any number of arguments, parameter will work like array. But we don't have to pass array explicitly,
  • Ex: See max() in Test.java,
    private static int max(int... arr){
        if(arr.length == 0) return 0;
        int mx = Integer.MIN_VALUE;
        for(int num : arr){
            if(num > mx) mx = num;
        }
        return mx;
    }
    
    Using like this:
    System.out.println( max() ); // 0
    System.out.println( max(1,22) ); // 22
    System.out.println( max(1,2,3,4,5,6,7,8,10) ); // 10
    
  • Isn't it awesome?
  • There is 2 restriction:
    • A varargs method can have maximum one varargs,
      // n1 has infinite length, so n2 is not needed. error
       void m1(String str, int...n1, int...n2) {
        ...
      }
      
    • The varargs must be the last argument in the argument-list. Same reason even though parameter type is different.
      void m2(int...n1, String str) {
       ...
      }
      
      This is perfectly valid: See findMinMax() Test.java,
      private static int findMinMax(boolean findMax, int ...arr){...}
      

Generic class

  • Allows for writing true polymorphic code(Works for any types),
  • Structure:
    public class Wrapper<T> {
     // Code for the Wrapper class goes here
    }
    
  • T is a type variable. It can be of any type, but must be reference type,
  • Ex: See MyList.java,
    public class MyList <T>{
        private final List<T> list = new ArrayList<>();
        
        public MyList() {}
    
        public void add(T item){
            list.add(item);
        }
    
        public T get(int index) throws ArrayIndexOutOfBoundsException{...}
    
    }
    
    Using like this: See simpleGenericTest() in Test.java ,
    private static void simpleGenericTest(){
      MyList<Integer> myList = new MyList<>();
      //MyList myList = new MyList<>(); // ok but not recommended.
      myList.add(12);
      myList.add(32);
      myList.add(42);
      myList.add(62);
    
      //System.out.println( myList.get(5) ); // Index out of bound
    
      System.out.println( myList.get(1) ); // 32
    }
    
  • Above one is a simple example. Learn more by yourself if you want to,
User12024-11-02

This is a comment 1.

User12024-11-02

This is a comment 2.