Posts

Day32: Method Overriding

 Method Overriding Whenever a child class has method with SAME PROTOTYPE as parent class METHOD, then we say that the child class has overridden the parent class method. class A { public void show() { ............. ............. ............. } } class B extends A { public void show() { ................. } } Prototype: Access modifier, return type, Argument list, Name Both the classes access modifier is same. Both the classes have return type is Void. Both the classes show name is show(). Both the classes show is non parameterized. It means Class B 'show()' overide the Class A 'show()'. Why do we perform method overriding? Sometimes it happens that the functionality (logic ) of  a method defined in Parent class may not be suitable for the child class. For Example : The logic of sound() method in the class CAT is not suitable for the class TIGER because Tiger makes a different sound. In this case the programmer of TIGER class must compulsory overr

Day31: Inheritance, Super()

Image
 Inheritance Inheritance is one of the core principles of Object Oriented Programming. As the name indicates to inherit means to acquire features of existing entity in a newly created entity. Just like a child inherits the features of his/her parents. Similarly, if while designing a class in Java, a programmer wants then he can use the features(instance variable and Methods) of an existing class into his own class. But to do this the programmer will have to apply the concept of INHERITANCE. In java, the class will gets inherited is called as SUPER CLASS and the class which inherits is called as SUB CLASS. Thus via inheritance member of super class can also be accessed through the object of sub class. Benefits of Inheritance The major benefit of applying inheritance is code reusability. This is because the sub class programmer is not required to redesign those methods or java again which already have been provided by the super class. This saves lor of time and effort on the part of sub

Day30 : this reference

 The 'this' Reference 'this' is a special implicit reference which is available in parameter list of every instance in Java. Whenever we call any non-static Or instance method of any class then java automatically passes the addresses of the calling object as argument to that method. Within this method java itself declares a special reference in the parameter list and this reference is called "this" reference. so, we can say that every non - static method(including constructor) always knows the address of its calling object via its "this" reference. "this" is also a keyword in java. Benefit of using "this" By using "this" we can resolve the overlapping of data member/instance var of the class done by local var of the method with same name. public class Emp{          private int id;          private String name;          private double salary;                    public Emp(int id, String name, double salary)          {   

Day29 : Static Block

 Static Block 1) A Static Block in Java in a block which is created outside any method but inside class Body using the keyword static. 2) The general sentence of writing static block is      class<class Name>     {          static          {               ..........               ..........          }     Other member of the class     } 3) Java executes static block only one and automatically. 4) This happens when we make 1st use of the class. What is the first use of the class In Java the first use of the class when either of the following 2 situation occur: 1) We make the first object of the class Or 2) We access any static member of that class for the 1st time Whichever of the above 2 happens 1st, it is called 1st use of the class. What java does when we make 1st use of the class? Whenever we make 1st use of the class Java does 3 thing: 1) It loads the class in memory 2) If the class contains any static member then it allocate space for that static member in method area. class

Day28 : Using Static Method

 Using Static Method In Java just like we can have static data similarly we also can have static method in our class and java recommends two situation when we should make our method static. 1) When we've a method in the class which is only accessing static data member of the class. For Example : In the previous program the method showNextId() must be declared as static because it is only accessing static data nextId in its body. The most important benefit of doing this will be that now we can call the method showNextId() without using any object by simply using class name. code:     public static void showNextId() {     System.out.println("Next emp id will be " + nextId); } call :      Emp.showNextId(); 2) Whenever we've a class without any instance variable then all the method of that class must be declared as static. For Example :  public class MyMath() {     public static void main(String [ ] args)     {          int c;          c = a + b;          return c;    

Day26 : Object Class and Initialize Method

 Object Class and Initialize Method In Java, there is a special class called object available in the package java.lang . Specially of this class in that every java class which does not inherit of any other class automatic inherit of any other class. In Other word we can say every class in java directly or indirectly becomes child class of object class. The class object has a bunch of useful method predefined into it by java. And since every class inherit the class object so these method are inherit by every other class in java. Amongst these method, there is a special method called finalize which has been following body in object class. protected void finalize() { } The finalize method is automatically called by JVM during object destruction i.e., when the garbage collector remove an object from heap area, then for every object it calls finalize method. So, if we want to execute some piece of code during object destruction then we should overwrite finalize method in our class as shown

Day25: Using the Static Keyword

Image
 Using the Static Keyword The static keyword is also called as non-access modifier. In java it is used in 3 places: We can declare "Static" data member. We can declare "Static" method. We can have static initializer block in Java. Special Note : In Java we cannot declare a local var as static. class Data {     int a;  //instance variable     int b; //instance variable } The variables a and b are called non-static OR instance. They will be allocated space in Heap Area, the moment an object or data class gets created. They will have as many copy created as we will create object of data class. i.e., for every object of data class a and b will have separate copy. class Data { int a; static int b; } In Above code, the data member 'a' is still called instance variable. But the data member 'b' is  called static OR class level OR shared variable. The Static variable 'b' will be loaded in memory as soon as the data class will be used by the progra