Posts

Showing posts from March, 2017

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 class needs to have a main() method                  public class HelloWorld                   {                     public static void main(String[] args)                        {                         

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;}    }        class Test{         public static void main(String args[]){              Car b;              b=new Audi();              System.out.println("Audi Price is: "+b.getAmount()+"Cr&qu

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[]){              Bank b;              b=new Savings();              System.out.println("Savings Balance is: "+b.getBalance());                b=new Current();              System.out.println("Current Balance is: "+b.getBalance());            }    } Output :