Java

Concepts 






    Method overriding 


    Overriding a method is a concept that allows us to declare a method in subclass even though the method is already present in parent class .
    Example:
    This can be seen in the following example .
    Audi class here extends Car class.
    Note here that both the classes Audi and Car, have a common method void speed(). Audi class gives here its own implementation to the speed()method .
    That means Audi class is overriding the method speed().
    class Car{
       public void speed()
       {
          System.out.println("Car runs at a high speed");
       }
    }
    class Audi extends Car{
       public void speed (){
          System.out.println("Audi is the fastest");
       }
       public static void main( String args[]) {
          Audi obj = new Audi ();
          obj. speed ();
       }
    }


    Output:
    Audi is the fastest  


    What are the Advantages of method overriding ?
    Child class can have its own implementation for an inherited method without modifying the method in the parent class/base class .


    Method Overriding in dynamic method dispatch
    We can assign the base class reference to a child class object .This technique is called Dynamic method technique .
    In the example below that the base class reference is assigned to child class object.
    Class MyParentCar{  
       public void disp() = displayColor
       {
          System.out.println("displayColor() method of parent class");
       }
       public void sound()
       {
          System.out.println("sound() method of parent MyParentCar  class");
       }          
    }


    class ChildAudi extends MyParentCar {
       public void displayColor (){
          System.out.println("displayColor() method of Child Audi class");
       }
       public void speed(){


          System.out.println("speed () method of Child class");
       }


       public static void main( String args[]) {
          //Parent class reference to child class object
          MyParentCar obj = new ChildAudi ();
          obj.displayColor();
          obj.sound ();
       }
    }


    Output:
    displayColor() method of ChildAudi class
    sound() method of parent MyParentCar  class  


    Note: We can only call methods which are lready prsent in the Basr Class also.
    We will not be able to call speed() because it is not present in the base class.
    if we try to call the speed () method with obj. speed() ,  we would get compilation error as follows:
    Exception in thread "main" java.lang.Error: Unresolved compilation
    problem: The method speed() is undefined for the type MyParentCar




    Abstract Classes and Methods


    Abstraction is a process of hiding the implementation details and showing only functionality to the user.
    Another way, it shows only important things to the user and hides the internal details .
    While sending an email we create the body of the mail and subject , and the email address to which we want it to be delivered.
    We don't exactly know the internal process of how the e-mail is being delivered to the reciever.
    Abstraction lets us focus on what the object does instead of how it does it.
    Abstract Class
    A class that is declared using “abstract” keyword is known as abstract class  
    It needs to be extended and its method implemented. It cannot be instantiated.


    There are two ways to achieve abstraction in java
    1. Abstract class offers partial (0 to 100%) abstraction
    2. Interface offer (100%) abstraction
    It may or may not include abstract methods which means in abstract class you can have concrete methods (methods with body) as well along with abstract methods ( without an implementation, without braces, and followed by a semicolon). 


    Example of Abstract class
     // Lines of code below show an abstract class declaration using keyword “abstract”


    abstract class MyAbstractClass{
       //line of code below shows an Abstract method: without body and braces
       abstract public void myAbstractMethod(); 


       // lines of code below show a Concrete method: with body and braces
       public void myConcreteMethod(){
          //Write your statements here
       }
    }


    Note : while declaration of an abstract class
    1.    Declare a class as abstract class if it has both abstract methods and concrete methods: If the class is having only abstract methods: declare it as interface.


    2.    Declare a class as interface if it has only abstract methods.


    Important points
    1. Always declare a class abstract when there is an abstract method inside that class .
    2. An abstract has to be always extended by some other class.
    3. An abstract class may not have any abstract method .
    4. An abstract class can have non-abstract methods (concrete) as well.
    5. non-abstract class will have only non-abstract methods (concrete) .
    6. A non-abstract can NOT have an abstract method