Posts

this in java

this is a keyword in Java.It can be used inside the method or constructor of  Class.Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this . Here are the points where the keyword this can be used in Java :  Within the method of current class.  Within the constructor of current class.  To refer current class instance variable.  Passed as arguments to call constructor.  Passed as arguments to call method.  To invoke the method of current class.  To invoke the constructor of current class.   Example :            1.  Without this                            // one cl...

Interface in java

The interface keyword is used to declare an interface. An interface  is a blueprint of a class. We can't create an instance of interface . By using interface , we  can achieve abstraction in java  By using interface , we can implement the functionality of multiple inheritance. It does not contain any constructor. All the methods are abstract method in interface. An interface can't be extends by a class but extends by interface . An interface can't be implemented by interface but implemented by a class. Example :   interface   Car{             float getAmount();        }   class Audi implements Car{            public float getAmount(){return 2.5f;}       }   class Bmw implements Car{         public float getAmount(){return 80.3f;} ...

Abstract class in java

A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods.  If a class has at least one abstract method then the class must be declared as abstract .  abstract class can't be initiated . To use an abstract class , you have to extends it to another class and must be implement all the abstract method .  Example :   abstract class Bank{            abstract int getBalance();        }   class Savings extends Bank{            int getBalance(){return 40;}       }   class Current extends Bank{          int getBalance(){return 200;}    }        class Test{         public static void main(String args[]){...

Types of Inheritance in Java

There are five types of inheritance . Types are 1)Single 2) Multilevel 3) Multiple 4) Hybrid 5) Hierarchical . But Java supports four types of inheritance . Java does not support " Multiple Inheritance ". 1) Single :                 public class A{              ................           }          public class B extends A{                        .................          }          Supports     2) Multilevel :         public class A{              ................    ...

Inheritance in Java

Inheritance   is one of the feature of Object-Oriented Programming . Inheritance allows a class to use the properties and methods of another class. When a class "ABC" can access  the properties and method of a class "XYZ" then the "XYZ" class is called " Parent/Super " class and the "ABC" class is called " Sub/Derived " class . Syntax is     class XYZ{       //  write your logic here    }  class ABC extends XYZ{     // write  your logic here } Example : class A  {     int x;      public void display()      {           System.out.println("Value of X is :"+x);      } } public class B extends A{     public static void main(String []args)   {      A obj=new A();      obj.x=10;      obj.display();   ...

continue and break statement

continue :           Continue statement is used to skip the execution of code in loop . Continue statement is used only inside the loop control(while,do-while,for) in java . continue is a keyword in java. break :        break statement is used to terminate the execution of code. break is a keyword in java. Let's see an example of continue and break statement. Without continue and break statement (Fig-1): public class ContinueBreakExample {   public static void main(String[] args)   {     for(int i=0;i<3;i++){        System.out.println(i);     }   } } Output :               0               1               2 With continu...

switch statement in java

switch statements is similar to if-else-if ladder in java . It execute one statement from multiple conditions. A switch statement have an optional default case which can be appear at the end of switch . Default case will execute when none of conditions are matched . Default case has no break statement . Each case must have break otherwise the flow of control will fall until a break is reached . switch works with byte,int,short,char primitive data types . It also works with Enum types , String class etc.   Syntax : switch(expression) {   case value : // statements  break;  case value :    // statements    break;    default : // statements }    Example :    public class SwitchDemoExample { ...