❮ Previous Next ❯

Java Inheritance


      class Vehicle
      { 
       // code
      }
      class Bike extends Vehicle
      {
      // extends the property of Vehicle class
      }



InheritanceExample1.java

package java8s;

class A1
{
      void m1()
      {
            System.out.println("java8s.com");
      }
}

class B extends A1
{
      void m2()
      {
            System.out.println("SILAN TECHNOLOGY");
      }
}

public class InheritanceExample1 {

      public static void main(String[] args) {
           
            //Case-1
                    A1 ob1=new A1();
                    ob1.m1();
                    ob1.m2();    //invalid            

            //Case-2  (Best Suitable Case)
                    B ob2=new B();
                    ob2.m1();
                    ob2.m2();
           
            //Case-3
                   A1 ob3=new B();
                   ob3.m1();
                   ob3.m2();    //invalid            

            //Case-4
                   B ob4=new A1();    //invalid

      }

}

Output
Inheritance

NOTE:
From the above 4 cases we observed that Case-2 is the best case, that means through the sub class object we can access both super and sub class properties.



InheritanceExample2.java

package java8s;

class A
      {
            int X;
      }
class B extends A
      {
            int Y;
            void get()
           {
             X=100:
             Y=200:
           }
           void show()
           {
             System.out.println("X="+X);
             System.out.println("Y="+Y);
           }
      }
     public class InheritanceExample2 {

           public static void main(String[] args) {
             Bobj=newB();
             obj.get();
             obj.show();
           }

           }

Output
Inheritance

NOTE:
Here from this program we observed that the sub class property can access super class property.


❮ Previous Next ❯









  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved