Search from 700 + Posts

Feb 22, 2011

Sample Java Program to Extend Class

In this Code I am extending Class srt (Super-Class ) in srclass1 (sub-Class)

package java_test1;

import java_test.srt;  //I am using other Class , just line another Pl/sql API
//Extends means I am extending Methods of srt in srclass1
public class srclass1 extends srt {
    public srclass1() {
        super(); //Callto Super Call Constructor
      super.pub_method();//call to Method in Super Class(Parent Class
    }
    //Over-ride Method in Sub-Class
    public void pub_method(int a){
      System.out.println("I am Publice Method from srclass1"+a);
      System.out.println("Sub Class Method should have same Signature for Override");
    }
   
public static void main(String[] args)
{
System.out.println("I am Main in Class srClass1");
 srclass1 lclass = new srclass1();
 //lclass.pub_method();
 lclass.pub_method(200);  //It will call Method in SubClass and not in Super Class
 if (lclass instanceof srclass1) {   //Example about onstanceof
   System.out.println("Yes I am instance of srClass1");
 }
 if (lclass instanceof srt){
    System.out.println("Yes I am instance of srt");
    }
 srt lclass1 = new srt();
 if (lclass1 instanceof srt){
  System.out.println("Yes I am instance of srt");
}
 if (lclass1 instanceof srclass1) {
   System.out.println("Yes I am instance of srt");
 }

  }
}

Code for srt class is available at  Class srt

No comments:

Post a Comment