Is it possible to OVERLOAD a super-class method in a sub-class?

Hi all,
I have a query that
Is it possible to OVERLOAD a super-class method in a sub-class?
If it is possible, please give me an example.
Thanks,
Hari

Hi,
Is the method int Display(int a){} overloading
the super-class's void Display() method? If
possible, please clarify this and how it would be
method overloading?
hanks,
Hari
Hi Hari,
Yes, it is possible. Look at this piece of code:
class Senior
     void Display()
          System.out.println("Super class method");
class Junior extends Senior
     int Display(int a)
          System.out.println("Subclass method: "+a);
          return(a+10);
     }> }
class example
     public static void main(String args[])
          Junior j = new Junior();
          j.Display();
System.out.println("Subclass method
od "+j.Display(5));
Is this what you were asking? Hope this helped.Hi,
I guess you guys are confused here...
Overloading is achieved by methods in the same class...
Overriding is across a superclass subclass methds.

Similar Messages

  • Can we chagne Super class method parameters in Sub class

    Hi,
    I created a super class.  In that class i created a method. That method is having 4 input parameters and 4 export parameters.
    I created a sub class for that super class. I need to use only 2 input parameters in this class rather than 4 parameters. I want to delete 2 Input parameters in the sub class of the super class method.  Is it possible.
    If possible. can we give an simple code or pseudo code?
    regards,
    krishna

    Hi,
    I think you can not.
    Because, only public attributes can be inherited and they will remain public in the subclass.
    for further detail check,
    http://help.sap.com/saphelp_nw70/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm
    regards,
    Anirban

  • Can we change the Super class attribute scope in Sub class

    Hi.
    I created a super class. In that i have 4 attributes. That attributes are PUBLIC.
    I created a sub class. In that i got all super class attributes. I want to change that attributes as a Private. Is it possible.
    If it is possible.Give me an Example with code or Pseudo code.
    Regards.
    Krishna.

    Hi Krishna,
    It is not possible... If you declare the Attributes again in Subclass of the same name as that of Super class
    then the way of accessing them would be different from that of attributes in the main class.
    Hope this would help you
    Good luck
    Narin

  • Static Classes/Methods vs Objects/Instance Classes/Methods?

    Hi,
    I am reading "Official ABAP Programming Guidelines" book. And I saw the rule:
    Rule 5.3: Do Not Use Static Classes
    Preferably use objects instead of static classes. If you don't want to have a multiple instantiation, you can use singletons.
    I needed to create a global class and some methods under that. And there is no any object-oriented design idea exists. Instead of creating a function group/modules, I have decided to create a global class (even is a abstract class) and some static methods.So I directly use these static methods by using zcl_class=>method().
    But the rule above says "Don't use static classes/methods, always use instance methods if even there is no object-oriented design".
    The book listed several reasons, one for example
    1-) Static classes are implicitly loaded first time they are used, and the corresponding static constructor -of available- is executed. They remain in the memory as long as the current internal session exists. Therefore, if you use static classes, you cannot actually control the time of initialization and have no option to release the memory.
    So if I use a static class/method in a subroutine, it will be loaded into memory and it will stay in the memory till I close the program.
    But if I use instance class/method, I can CREATE OBJECT lo_object TYPE REF TO zcl_class then use method lo_object->method(), then I can FREE  lo_object to delete from the memory. Is my understanding correct?
    Any idea? What do you prefer Static Class OR Object/Instance Class?
    Thanks in advance.
    Tuncay

    @Naimesh Patel
    So you recommend to use instance class/methods even though method logic is just self-executable. Right?
    <h3>Example:</h3>
    <h4>Instance option</h4>
    CLASS zcl_class DEFINITION.
      METHODS add_1 IMPORTING i_input type i EXPORTING e_output type i.
      METHODS subtract_1 IMPORTING i_input type i EXPORTING e_output type i.
    ENDCLASS
    CLASS zcl_class IMPLEMENTATION.
      METHOD add_1.
        e_output = i_input + 1.
      ENDMETHOD.
      METHOD subtract_1.
        e_output = i_input - 1.
      ENDMETHOD.
    ENDCLASS
    CREATE OBJECT lo_object.
    lo_object->add_1(
      exporting i_input = 1
      importing e_output = lv_output ).
    lo_object->subtract_1(
      exporting i_input = 2
      importing e_output = lv_output2 ).
    <h4>Static option</h4>
    CLASS zcl_class DEFINITION.
      CLASS-METHODS add_1 IMPORTING i_input type i EXPORTING e_output type i.
      CLASS-METHODS subtract_1 IMPORTING i_input type i EXPORTING e_output type i.
    ENDCLASS
    CLASS zcl_class IMPLEMENTATION.
      METHOD add_1.
        e_output = i_input + 1.
      ENDMETHOD.
      METHOD subtract_1.
        e_output = i_input - 1.
      ENDMETHOD.
    ENDCLASS
    CREATE OBJECT lo_object.
    lo_object->add_1(
    zcl_class=>add_1(
      exporting i_input = 1
      importing e_output = lv_output ).
    lo_object->subtract_1(
    zcl_class=>subtract_1(
      exporting i_input = 2
      importing e_output = lv_output2 ).
    So which option is best? Pros and Cons?

  • How to recognize the name of invoked classes&methods&fields in a class

    I am now doing some programming used to recognize the name of all classes, methods, constructors and fields invoked from other classes in a given class. This recognition is required automatical. Now I really have no idea how to realize it. Can anyone please give me some suggestions how to programme it?
    Now I show you a specific example to make sure you understand my question.
    From the following example, firstly I've no idea what outer classes, methods and fields are used in class "PointShadowProtocol". However, the expected functionality of realization is to find out: 1, the name of two used outer classes: Shadow, Point; 2, invoked constructor: Shadow s=new Shadow(int x,int y); 3, invoked methods: Point.getX()&#65292; Point.getY()&#65292; Point.printPosition()&#65292;Shadow.offset&#65292;Shadow.printPosition(); and 4, invoked field: Point.x,Point.y,Shadow.x,Shadow.y
    public class PointShadowProtocol{
    private int shadowCount=0;
    public static Shadow getShadow(Point p){
    Shadow s=new Shadow(p.x,p.y);
    return s;
    public void setting(Point p){
    Shadow s=new Shadow(p.x,p.y);
    shadowCount++;
    public void settingX(Point p){
    Shadow s=getShadow(p);
    s.x=p.getX()+Shadow.offset;
    p.printPosition();
    s.printPosition();
    public void settingY(Point p){
    Shadow s=getShadow(p);
    s.y=p.getY()+Shadow.offset;
    p.printPosition();
    s.printPosition();
    Actually, after realizing this functionality, I will use these results to automatically generate the related class stub, method stub, field stub, which can be used to test the given class "PointShadowProtocol", probably equivalent to unit test.
    Any suggestions are welcome. Thank you in advance for your reply.

    Using BCEL sounds a good idea for a class in Java. Actually, I want to target an aspect, which is from AspectJ, without knowing any invoked classes, class methods and fields inside an aspect in the first place (examples of an aspect is as showed below). An aspect in AspectJ is just like a class in Java. But an aspect can't be compiled if the invoked outer classes and methods don't exist. (In fact, the weaving of an aspect into classes happens in the compile time, an aspect can't be compiled if the woven classes (or invoked classes) don't exist, which means I have to find out the all the invoked classes, class methods and fields in that aspect before the compile time) So BCEL could not apply into an aspect in AspectJ.
    I am sorry to introduce new concepts here. However,the solution to find out all the invoked outer classes, class methods and fields in a given class before the compile time can be applied to an aspect as well.
    Thank you for your time to think about my question.
    public aspect PointShadowProtocolAspect {
         private int shadowCount=0;
         public static Shadow getShadow(Point p){
              Shadow s=new Shadow(p.x,p.y);
              return s;
         pointcut setting(Point p): target(p)&&call(Point.new(int,int));
         pointcut settingX(Point p):target(p)&&call(void Point.setX(int));
         pointcut settingY(Point p):target(p)&&call(void Point.setY(int));
         after(Point p): setting(p){
              Shadow s=new Shadow(p.x,p.y);
              shadowCount++;
         after(Point p):settingX(p){
              Shadow s=getShadow(p);
              s.x=p.getX()+Shadow.offset;
              p.printPosition();
              s.printPosition();
         after(Point p):settingY(p){
              Shadow s=getShadow(p);
              s.y=p.getY()+Shadow.offset;
              p.printPosition();
              s.printPosition();

  • Method of JUErrorHandlerDlg sub class

    Hi Forum
    i am working jClint/Swing Application in jDevloper.And
    i want to use own custom messageBox rather than exception
    through from database BC4J.
    I fund JUErrorHandlerDlg sub class for exception through
    so how to method use for this.
    please reply me some code.
    javed

    Thanks! for reply
    But until i found problem ,how to catch exception
    throw by bc4j(Jbo error meassage),exception has a
    error number(like jbo-10426).
    So using this error number how to use
    this exception,In order to own message dilog.
    please reply me some code,
    use in our class as
    public class subJUErrorHandlerDlg extends
    JUErrorHandlerDlg
    public subJUErrorHandlerDlg()
    // i write bootstrap application
    JUMetaObjectManager.setBaseErrorHandler(new subJUErrorHandlerDlg());

  • How to inherit super class constructor in the sub class

    I have a class A and class B
    Class B extends Class A {
    // if i use super i can access the super classs variables and methods
    // But how to inherit super class constructor
    }

    You cannot inherit constructors. You need to define all the ones you need in the subclass. You can then call the corresponding superclass constructor. e.g
    public B() {
        super();
    public B(String name) {
        super(name);
    }

  • Assigning a super class attributes to a sub class in an OO way

    I think you OO and Pattern forumites might want to help me out on this one.
    I started this in the beginners forum, much heat, but not too much light
    http://forum.java.sun.com/thread.jsp?forum=54&thread=439107
    Make sure you at least read past peoples initial misunderstanding of the problem!
    I get handed an object of type Car, and wish to create a new object of type Ferari which is simply an extension of a type Car class object. How can I best do this? Normally I start with the extended type of object and 'fill' that objects attributes, but on this occasion, I receive a simpler object and wish to create a specialised object based on it.
    The problem it seems is that assigning just the base object doesn't seem to do it (all fields are null).
    // Carpublic class Car {  private int engineSize;  // More code here}// Feraripublic class Ferrari extends Car {  private int turboChargers;          /**      * Constructor      */     public Ferrari(Car car) {// This doesn't seem to work//     this = (Ferrari) car;                 // Hmmm.  Tiresome and error-prone assignments     setEngineSize(car.getEngineSize());     setColor(car.getColor());          // etc.        }}

    sure, but the decorator would still BE a JFrame (or
    more likely a container)No, in the examples I have seen the Decorator is outside the heirarchy.
    Given your many useful posts, I highly doubt that
    you're missing a key point in understanding the
    decorator pattern.I have to admit that I am not as strong on patterns as I should be. I tend to learn things as I need them. The problem is often you don't realize you need patterns until you learn about them.
    MY opinion is that there can be no hard, strict
    definition of any pattern; as they are (in Fowler's
    words) "half-baked", and require modification to be
    implemented in any specific problem.I'll by that.
    Is my code a decorator? Yes, in my opinion. I also
    think its a delegate (though many would dispute that
    saying the delegatee "must" have a reference to the
    delegator to be a "true" delegate pattern).I've always called this Composition or a Wrapper. The link I found that agreed with your example actually said that Wrappers and Decorators are synonymous. To me, these seem like very different approaches. That's why I am wondering if my understanding of the Decorator pattern is incomplete or too narrow.
    Could you please point me to those references?I think these are two. If you find something that shows your example fits these definitions please let me know.
    http://web.media.mit.edu/~tpminka/patterns/Decorator.html
    http://www.wikipedia.org/wiki/Decorator_pattern

  • Assigning a super class attributes to a sub class

    I get handed an object of type Car, and wish to create a new object of type Ferari which is simply an extension of a type Car class object. How can I best do this? Normally I start with the extended type of object and 'fill' that objects attributes, but on this occasion, I receive a simpler object and wish to create a specialised object based on it.
    The problem it seems is that assigning just the base object doesn't seem to do it (all fields are null).
    // Car
    public class Car {
      private int engineSize;
      // More code here
    // Ferari
    public class Ferrari extends Car {
      private int turboChargers;
          * Constructor
         public Ferrari(Car car) {
    // This doesn't seem to work
    //     this = (Ferrari) car;            
         // Hmmm.  Tiresome and error-prone assignments
         setEngineSize(car.getEngineSize());
         setColor(car.getColor());
           // etc.
    }     Thanks for any suggestions!

    Wait - sorry, it's more like a copy constructor. I missed that the first time through.
    There's no good way around it - you'll have to get those values in there somehow. Unless there's a reasonable default value for all the Ferrari members that aren't part of Car, you'll have a problem with this because of the slicing problem.
    It's as if you're writing a C++ copy constructor. It's just an alternative to the Java clone method.
    If there are really that many attributes, maybe you can assign them using the Java reflection API.
    Unless there are hundreds of them, type them once and be done with it. If there ARE hundreds of them, you might want to consider refactoring your class. It's too big!
    You'll have to do that kind of thing when you write the equals and hashCode methods for your classes. Just do it! - MOD

  • Overriding super class methods @ OIM 9x

    Hi All,
    I'm working on a requirement where I need to override superclass method in sub class.The problem is, method defined in sub class is not getting called by OIM.
    My question more looks like Pure Java related,but as it is OIM which uses Struts, calling of ActionForward methods defined in
    protected Map getKeyMethodMap()
    +{+
    +}+
    Super class is OUT OF BOX class and sub class is I m developing.Both superclass & subclass having the protected Map getKeyMethodMap() method with same declaration.
    Can any one give me a clue on this.
    Regards,
    Krish

    HI All,
    Issue resolved after deleting stage & tmp folders in weblogic instance directory and restarted weblogic instance.
    Regards,
    Krish

  • Inner classes can't access parent classes in constructor

    I'm having a problem where I have class A, which has an Inner class B, which has it's own inner class C. In C's constructor (the inner most class), i'm trying to access a method of A (the top most class), and I get a NullPointerException with trace:
    at mearns.finance.DefaultPortfolio.access$0(DefaultPortfolio.java:1)
    at mearns.finance.DefaultPortfolio$AccountsEditor$AccountsTableModel.<init>(DefaultPortfolio.java:253)
    In this case "DefaultPortfolio" is class A, AccountsEditor is class B, and AccountsTableModel is class C. The line given in the second stack trace element is the line in C's constructor which calls Class A's method.
    Debugging, I stop inside C's constructor. Before anything happens, debugger says this$1 is null. Then I step, and it calls super(), and now this$1 is an instance of class B (AccountEditor), but it's own this$1 (which should be an instance of class A) is still null.
    I'm calling C's constructor from within B's constructor (but not A's), not sure if that makes a difference.
    Can anyone explain what's going on here, and (hopefully) how I can work around it?
    Thanks for any help.
    When i debug

    hm, I really don't know what is happening but I want to tell you about something that is not nice in your code and possibly it could cause the error.
    Code behaves somwhat strange when you pass this out of a constructor. You do this implicitly when you create an inner classe which implicitly gets a reference to the outer class. Also just simple method calls can cause such effects.
    This is because the construction process it not finished but methods are already called.
    Even worse: the sub classes constructor possibly did not even start. Special to java is the fact that a method call can lead to an overridden method of a sub class who's constructors has not even passed the super() command.
    My guess now is that you have that some kind of this situation in your code. You create an inner class in a constructor. That inner class calls back a method on the outer class. Now say that the method you are calling is defined in a sub class who's constructor still stucks in super() call. There we are.
    I found a thread in a news group that covers exactly this problem:
    It is therefore a good idea only to call private methods from constructors (that should then also call only private methods). Also if inner classes are created in the constructor then they should not call any non trivial / non private methods of the outer class. Calling overridden methods out of constructors should be avoided strictly!
    Perhaps this has nothing to do with your problem. I cannot tell for sure.
    But this sounds quite interesting.
    Please tell me if my guess was right and if not post simple code the illustrated your problem.
    Here a simple example also throwing NullPointerException:
    class Outer {
      int i = 42;
      abstract class InnerSuper {
        InnerSuper() {
          foo();
        abstract void foo();
      class InnerSub extends InnerSuper {
        void foo() {
          System.out.println(i);
      public void bar() {
        new InnerSub();
      public static void main(String[] args) {
        Outer o = new Outer();
        o.bar();
    } It causes a NullPointerException because the InnerSub constructor did not run. I copied the example from a news group thread i found.
    http://groups.google.de/group/comp.lang.java.programmer/browse_thread/thread/897fba792d689b29/a1ba2ed708636a30?q=inner+class+outer+this+reference+NullPointerException&rnum=4&hl=de#a1ba2ed708636a30
    Here is a even simpler example of strange behavior - even without NullPointerExceptions.
    class A {
         A() {
              System.out.println(getName());
         String getName() {
              return "A";
    class B extends A {
         String NAME = "B";
         String getName() {
              return NAME;
    new B();It will output null instead of B.
    regards
    Sven

  • What are abstract classes/methods and what are they for?

    Hi,
    I've just heard about abstract classes and methods and I'm just wondering what exactly they're used for, and why are they there for the Graphics class for example?
    Cheers.

    raggy wrote:
    bastones_ wrote:
    Hi,
    I've just heard about abstract classes and methods and I'm just wondering what exactly they're used for, and why are they there for the Graphics class for example?
    Cheers.Hey bro, I'll try to solve your problemYou have to know two important concepts for this part. 1 is Abstract classes and the other is Interface classes. Depends on the nature of the project, you need to set certain level of standards and rules that the other developers must follow. This is where Abstract classes and Interface classes come into picture.
    Abstract classes are usually used on small time projects, where it can have code implementation like general classes and also declare Abstract methods (empty methods that require implementation from the sub-classes).Wrong, they are used equally among big and small projects alike.
    Here are the rules of an Abstract class and method:
    1. Abstract classes cannot be instantiatedRight.
    2. Abstract class can extend an abstract class and implement several interface classesRight, but the same is true for non-abstract classes, so nothing special here.
    3. Abstract class cannot extend a general class or an interfaceWrong. Abstract classes can extend non-abstract ones. Best example: Object is non-abstract. How would you write an abstract class that doesn't extend Object (directly or indirectly)?
    4. If a class contains Abstract method, the class has to be declared Abstract classRight.
    5. An Abstract class may or may not contain an Abstract methodRight, and an important point to realize. A class need not have abstract methods to be an abstract class, although usually it will.
    6. Abstract method should not have any code implementations, the sub-classes must override it (sub-class must give the code implementations). An abstract method must not have any implementation code code. It's more than a suggestion.
    7. If a sub-class of an Abstract class does not override the Abstract methods of its super-class, than the sub-class should be declared Abstract also.This follows from point 4.
    9. Abstract classes can only be declared with public and default access modifiers.That's the same for abstract and non-abstract classes.

  • How to call inner class method in one java file from another java file?

    hello guyz, i m tryin to access an inner class method defined in one class from another class... i m posting the code too wit error. plz help me out.
    // test1.java
    public class test1
         public test1()
              test t = new test();
         public class test
              test()
              public int geti()
                   int i=10;
                   return i;
    // test2.java
    class test2
         public static void main(String[] args)
              test1 t1 = new test1();
              System.out.println(t1.t.i);
    i m getting error as
    test2.java:7: cannot resolve symbol
    symbol : variable t
    location: class test1
              System.out.println(t1.t.geti());
    ^

    There are various ways to define and use nested classes. Here is a common pattern. The inner class is private but implements an interface visible to the client. The enclosing class provides a factory method to create instances of the inner class.
    interface I {
        void method();
    class Outer {
        private String name;
        public Outer(String name) {
            this.name = name;
        public I createInner() {
            return new Inner();
        private class Inner implements I {
            public void method() {
                System.out.format("Enclosing object's name is %s%n", name);
    public class Demo {
        public static void main(String[] args) {
            Outer outer = new Outer("Otto");
            I junior = outer.createInner();
            junior.method();
    }

  • How to get a int value from another class method?

    Hi,
    how can I get a value of another class method variable value.
    example,
    class elist
            int a;
         ArrayList<Event> eventArray;
         void addEvent(Event e);
         Event getEvent(int index);
         void removeEvent(int index);
         void orderEventByTime();
    interface Event
         void Command();
    class servo implements Event
         String ip;
         int time = 10;
         void Command();
    class servo_2 implements Event
         String ip;
         int time = 20;
         void Command();
    [\code]
    I want to get the time value in elist variable a; 
    and want to compare each class time?.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Hi,
    1) this foum provides means to format/tag code, no need to manually add -tags
    2) by default, classname start with a capital letter, method names with a lower case letter
    3) where do you want to get the time value to Elist.a? During addEvent()?
    4) what do you want to do with the time value of each event? Sum all values up to make a an overall sum?
    5) where do you want to compare the time value(s)?
    To put it in one sentence: please be more specific with your description and answer.
    Bye.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Code to call friends class method

    Hello,
    Please can someone help me with friends class method call. in global classes
    Thanks
    Megh
    Moderator message: please search for available information/documentation before asking.
    Edited by: Thomas Zloch on Nov 2, 2010 5:29 PM

    What is your exact problem?
    The class whose private and protected section you want to use, needs declaration of its friends. You do it in Friends tabstrip. All these classes are allowed then to access these "hiden" sections from outside of the class. Below example how it works with local classes. For global ones the principle stays the same
    CLASS lcl_main DEFINITION DEFERRED.
    CLASS lcl_other DEFINITION FRIENDS lcl_main. "only LCL_MAIN can use may hiden sections
      PROTECTED SECTION.
        METHODS prot_meth.
      PRIVATE SECTION.
        METHODS priv_meth.
    ENDCLASS.                  
    CLASS lcl_other IMPLEMENTATION.
      METHOD prot_meth.
        WRITE / 'This is protected method of class lcl_other'.
      ENDMETHOD.                    "prot_meth
      METHOD priv_meth.
        WRITE / 'This is private method of class lcl_other'.
      ENDMETHOD.                    "priv_meth
    ENDCLASS.                   
    CLASS lcl_main DEFINITION.
      PUBLIC SECTION.
        METHODS call_friends_methods.
      PRIVATE SECTION.
        DATA mr_my_friend TYPE REF TO lcl_other.
    ENDCLASS.                  
    CLASS lcl_main IMPLEMENTATION.
      METHOD call_friends_methods.
        CREATE OBJECT mr_my_friend.
        mr_my_friend->prot_meth( ).
        mr_my_friend->priv_meth( ).
      ENDMETHOD.                   
    ENDCLASS.           
    START-OF-SELECTION.
      DATA gr_main TYPE REF TO lcl_main.
      CREATE OBJECT gr_main.
      gr_main->call_friends_methods( ).
    Regards
    Marcin

Maybe you are looking for

  • Downloading and reinstalling Adobe Photoshop CS6. Backing up Photoshop CS6 for reinstall

    I had to return my cintiq companion HD tablet and will have to reinstall all of my software once i get it back now. I can't find where to download and install my CS6. I'd also like to create a backup for if this happens again, so I don't have to find

  • RS422 on RPI

    I need to send data to a device that talks RS422 from the RPI.  My plan would be to use a USB to RS422 adapter.  I am programing in Java ME.  I am new to the RPI and Java ME and have never used serial ports on a linux OS.  Could anyone give me some s

  • SRM5.0: table BBP_PDPSET to upadte

    Hi SRM Masters! Is there a particular way (FM, BADI...) to update table BBP_PDPSET? Thanks in advance. PT.

  • A simple question regarding c:out and ADF

    I have a small problem which I can't figure out :-( The solution is probably dead easy, but I don't see it .. The problem is : I have a jsp-file which have the following tags : <c:out value="${bindings.EstimatedInterest}" /> <c:out value="${bindings.

  • How do I maximize webpagers within the browser

    On my old computer I had my webpages maximized to fit the screen. Right now for firefox is does it on some webpages not all. I do not remember how I changed it to do that on all webpages. Could you please send me an email how to do that