Java Concepts : Part - 03

Java Concepts : Part - 03

ยท

15 min read

So far w had covered the basic of Java programming, which will help you to understand some basic properties of Java and that how does Java language works. Now we will take a deeper dive in Java and will see some of the useful and important features of Java programming language.

  1. Static scope rule :

    • Instance variable :

      • Instance variable are declared in a class, but outside a method, constructor or any look.
      • When space is allocated for an object in the heap, a slot for each instance variable is created.
      • The instance variable are visible for all method constructors, and block in the class.
    • Class variable : Class variable also known as static variable decaled with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it.

Example :

public class VariableExample{
    int myVariable;
    static int data = 30;

    // Main method
    public static void main(String args[]) {
        int a = 100;
        variableExample obj = new VariableExample();

        System.out.println("Value of instance variable myVariable: " + obj.myVariable);

        System.out.println("Value of static variable data: " + VariableExample.data);
    }
}

Output :

Value of instance variable myVariable: 0
value of static variable data: 30
  1. static :

    • Java does not have global variable.
    • Every variable in Java be declared inside a class.
    • The keyword static is used to make a variable just like global variable.
    • A variable declared with static keyword is called class variable.
    • It acts like a global variable, that there is only one copy of variable associated with the class. That is, one copy of the variable regardless of the number of instance of the class.
  2. Nested class : Nested class means a class inside another class. We can define as many nested class as much we want. But we should try not to declare many nested class because it makes the code confusing and chances of getting error increase.

  3. Recursion in Java : Recursion in Java is a process in which a method is calls itself continuously. A method in Java that calls itself is called recursive method. It makes the code compact but complex to understand.

Example :

public class RecursionExample2{
    static int count = 0;
    static void p() {
        count ++;
        if(count <= 5) {
            System.out.println("Hello" + count);
            p();
        }
    }
    public static void main(String args[]) {
        p();
    }
}
  1. While loop v/s do-while loop : While loop checks the condition first and if the condition is true than only it runs, while on the other hand do-while loop run for the first time and after that it checks the condition.

  2. Inheritance in Java :

    • Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classification.
    • Using inheritance, one can create a general class that includes some common set of items.
    • This class then can be used to create more specific classes which has all the items from the base class, in addition to some items of it's own.
  3. Terms used in Inheritance :

    • superclass : A class that is inherited is called a superclass.
    • subclass :
      • The class that does inheriting is called a subclass.
      • A subclass is a specialized version of a superclass.
      • It inherits all of the instance variable and methods defined by the superclass and add its own, unique elements (i.e. variable and methods)
    • Reusability : It is a mechanism which facilitates you to reuse the data and methods of the existing class when one create a new class. One can use the same data and methods already defined in the previous class.
  4. Inheritance syntax : The extends keyword is used to define a new class that derives from an existing class. The meaning of extends is to increase the functionality.

    class <Subclass-name> extends <Superclass-name> {
     // Data and methods in this sub-class
    }
    
  5. Inheritance types :

    • Single Inheritance : When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance. Example :

      class Animal{
        void eat() {
            System.out.println("eating.");
        }
      }
      class Dog extends Animal{
        void bark(){
            System.out.println("barking.");
        }
      }
      class TestInheritance{
        public static void main(String args[]){
            Dog d = new Dog();
            d.bark();
            d.eat();
        }
      }
      

      Output :

      barking.
      eating.
      
    • Multiple single inheritance : To reduce the complexity and simplify the language, multiple inheritance is not supported in Java. Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error. Example :

      class A{
        void msg(){
            System.out.println("Hello");
        }
      }
      class B{
        void msg(){
            System.out.println("Welcome");
        }
      }
      class C extends A,B{
        // suppose if it were
      
        public static void main(String args[]){
            C obj = new C();
            obj.msg();  // Now which msg() method would be invoked ?
        }
      }
      

      Output :

      Compile Time error
      
    • Multilevel single inheritance : When there is a chain of inheritance, it is known as multilevel inheritance. As you see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance. Example :

      class Animal{
        void eat(){
            System.out.println("eating.");
        }
      }
      class Dog extends Animal{
        void bark(){
            System.out.println("barking.");
        }
      }
      class BabyDog extends Dog{
        void weep(){
            System.out.println("weeping.");
        }
      }
      class TestInheritance{
        public static void main(Strign args[]){
            BabyDog d = new BabyDog();d.weep();
            d.bark();
            d.eat();
        }
      }
      

      Output :

      weeping.
      barking.
      eating.
      
  6. Method Overriding concept :

    • Usage of Java method overriding : Method overriding is used to provide the specific implementation of a method is already provided by it's superclass. Method overriding is used for runtime polymorphism.
    • Rules of Java Overriding : The method must have the same name as in the parent class.
      • The method must have the same parameter as in the parent class.
      • There must be an IS-A relationship (inheritance).

    Note : A sub class object can reference a super class variable or method if it is not overridden.

    • A super class object cannot reference a variable or method which is explicit to the sub class object.
  7. super keyword concept in Java :

    • The super keyword in Java is a reference variable which is used to refer immediate parent class members.
    • Whenever you create an instance of a sub class, an instance of its parent class is created implicitly, which is referred by super keyword. Example-1 :
      class Animal{
        String color = "white";
      }
      class Dog extends Animal{
        String color = "black";
        void printColor(){
            System.out.println(color);
            System.out.println(super.color);
        }
      }
      class TestSuper{
        public static void main(String args[]){
            Dog d = new Dog();
            d.printColor();
        }
      }
      
      Output :
      black
      white
      
      Animal and Dog classes have a common property color. If you print color property, it will print the color of the current class by default. To access the parent property, you should use super keyword.

    Example-2 :

    class Animal{
        Animal(){
            System.out.println("animal is created");
        }
    }
    class Dog extends Animal{
        Dog() {
            super();
            System.out.println("dog is created");
        }
    }
    class TestSuper{
        public static void main(String args[]){
            Dog d = new Dog();
        }
    }
    

    Output :

    animal is created
    dog is created
    

    The super keyword can also be used to invoke the overloaded parent class constructor, if arguments are there, then they should be specified accordingly.

  8. Dynamic method dispatch concept :

    • Dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. Also, it is created Runtime polymorphism.
    • In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.
  9. Abstract concept :

    • Abstraction is a process of hiding the implementation details and showing only functionality to the user.
    • Abstraction let's you focus on what the object does instead of how it does it.
    • A class which is declared with the abstract keyword as an abstract class in Java. It can have abstract and non-abstract methods (i.e. method with the body only without its definition).
    • Points to remember :

      • An abstract class must be declared with an abstract keyword.
      • It can have abstract and non-abstract methods.
      • It cannot be instantiated.
      • It can have constructor and static methods also.
      • It can have final method which will force the sub class not to change the body of the method. Example :

        abstract class Bike{
          abstract void run();
        }
        class Honda extends Bike{
          void run(){
              System.out.println("Running safely");
          }
        
          public static void main(String args[]){
              Bike obj = new Honda();
              obj.run();
          }
        }
        

        Output :

        Running safely
        

        Here, Bike is an abstract class that contains only one abstract method run(). Its implementation is provided by the Honda class.

    Note : An abstract method should be defined in its sub class.

  10. final keyword concept :

    • The final keyword in Java is used to restrict the access of an item from its super class to a sub class. The Java final keyword can be used in many contexts.
    • Variable : A variable cannot be accessed in sub class.
    • Method : A method cannot called from sub class object.
    • Class : A class cannot be sub classes. Note : If you make any class as final, you cannot extend it.
  11. Concept of access modifiers : The access modifiers in Java specify accessibility (scope) of a data member, methods constructors or class.

This is the third part of the article series for Java Concepts, if you find it helpful than make sure to read the next article also. It is highly recommended that if you want to refresh the basic make sure to read previous articles also.

If you have any doubt, question or query related this topic, than feel free to contact me.

๐Ÿ“ฑ Contact Me

Twitter, LinkedIn, Telegram, Instagram,

๐Ÿ“ง Write a mail

rahulmishra102000@gmail.com

GitHub, HackerRank