Why to use Overriding?

1. If you did not like the functionality provided by parent class then you can change the function by overriding. 2. If you want parent class functionality and some extra functionalities provided by child class. Then also we can use overriding. Example:
                class Parent
                {
                     public void getCycle()
                     {
                       System.out.println("my cycle");
                     }
                    public void getBike()
                    {
                       System.out.println("Honda bike");
                    }
                }
               class Child extends Parent
               {
                   public void getBike() 
                   {
                       super.getBike();
                       System.out.println("pulsar bike");
                   }
                   public void getCar()
                   {
                       System.out.println("Nano car");
                   }
                }

                      main()
                      {
                           Child c=new Child();
                           c.getBike();   
                }
ploymorphism
--> Is-A relationship tells that you can create object for derived class and assign to a base class variable. Example:
         class Vehicle
         {
             public void run()
             {
               System.out.println("I can run");
             }
           }
           class Cycle extends Vehicle
           {
             public void run()
                {
                 System.out.println("I can run on 2 wheels");
               }
             }
            public class MainClass
            {
              public static void main(String[] args)
              {
                     Vehicle v=new Cycle();
                     v.run();
                     Cycle c= new Cycle();
                     c.run();
              }
            }
Note: When we are using Is-A relationship and calling overridden function then it will always call derived class function only. Example:
             class Parent
              {
                   public void getBike()
                   {
                     System.out.println("Suzuki");
                   }
               }
               class Child extends Parent
               {
                    public void getBike()
                    {
                        System.out.println("Pulsar");
                    }
                    public void getCar()
                    {
                        System.out.println("Nano");
                     }
                 }
                public class MainClass
                {
                     public static void main(String[ ] args)
                     {
                             Child c= new Parent();
                             c.getBike();
                             Parent p =new Child();
                             p.getBike();
                             p.getCar();
                            Child d= new Child();
                            d.getBike();
                            d.getCar();
                      }
                 }
-->If you are using Is-A relationship and if you try to call extra function added in derived class it will give compile time error. Overriding1.PNG

Hiding:

If you are having some static function in base and derived class with same signature then it is called as hiding.

Rules for hiding:

When you call a static function using a variable then it will call the function of the class for which you have created the variable. Example:
	        class Base 
	         {
  	              public static void disp()
 	              {

    	               
 	              public void test()
  	              {
   	                  System.out.println("Base test");
  	               }
	          }
     	         class Der extends Base 
	         {
  	               public static void disp()
  	               {
   	                    System.out.println("der static disp");
   	               }
	               public void test()
	               {
   	                    System.out.println("der test"); 
	                }
	          }
	        public class MainClass
	        {
	               public static void main(String[] args)
	               {
	                  Der d=new Der(); 
 	                  d.test();   
 	                  d.disp()//warning these are to be accessed with class not variable.
                          Der.disp();
   	                  Der.test();// compile error---not a static (i.e, only static can access with class name).
                                             
                       }
               }
Note: 1. We have to access class function or static function using class names only but it is not mandatory. 2. Since there is not much advantage of hiding it is not suggestable to use same static function in base and derived class. Previous Assignment:
       	     class ClientEnter
	     {
  	        public void done()
  	        {
    	           System.out.println("client work done");
  	         }
	     }
	     class Server
	     {
  	        public void run(ClientEnter c)
 	        {
   	             for(int i=0;i<10;i++)
   	             {
   	                 //doing client function
                     }
   	              c.done();
  	         }
	     }

	     class Client1 extends ClientEnter 
	     {
  	        public static void main(String[ ] args)
	        {
  	              Server s=new Server();
    	              ClientEnter d= new Client1();
                      s.run(d);
	        }
 	        public void done()
  	        {
 	             System.out.println("client1 work done");
  	         }
	      }
	     class Client2 extends ClientEnter
	     {
	         public static void main(String[] args)
 	         {
  	              Server t=new Server();
   	              ClientEnter e=new Client2();
    	              t.run(e);
           	}
 	        public void done()
 	        {
	             System.out.println("client2 work done");
	        }
	     }
--> In the above example the variable c in run functionality of server class is having polymorphic behavior (acting like more than 1 person) because if client1 passes his object then c.done() will execute client1's done functionality. --> Similarly, if client2 passes his object then c.done() will execute client2's done functionality. polymorhism1
polymorhism2