简体   繁体   中英

How can i extend function only instead of whole class in Java?

How can i extend functions only in Java? (not class extends)

/*Example: 
 * year 1990 Alex Kumar 
 * year 1990 Jhon Ya'ay 
 * made this: do not touch modify here */
public static void OldMade()
{
    Connect("+1800PIZZA");        
    Say("MYID:69"); 
    Request("PIZZACITROEN_BLACK");
}

/*Example: 
 * year 2011 Sunil Williams
 * applied which extends 1990 */
public static void MadeInIndia extends this.OldMade
{
    //hidden include Connect("+1800PIZZA");        
    //hidden include Say("MYID:69"); 
    //hidden include Request("PIZZACITROEN_BLACK");
    Send("CorrectionPandit"); 
}

private static void main(Strig args[])
{
    // Try 1 call and get old + new
    MadeInIndia();        

    //execute  Connect("+1800PIZZA");        
    //execute  Say("MYID:69"); 
    //execute  Request("PIZZACITROEN_BLACK"); 
    //execute  Send("CorrectionPandit");
}

Can't be done. Java's object-oriented; you only inherit and extend from classes and interfaces.

You don't "extend" static methods. In the following arrangement, class B's static version of foo will overshadow that from A. If B needs to call A's static method, it can do so explicitly:

public class A {

    public static void foo() { System.out("foo for A classes"); }
}

public class B extends A {

    public static void foo() { System.out("foo for B classes"); }
}

You can't extend a function. Why not just to write:

public static void MadeInIndia extends this.
{
    OldMade();
    Send("CorrectionPandit"); 
}

To extend static methods (as from your example) you just call the original method:

 public static void MadeInIndia {
   OldMade();
   Send("CorrectionPandit"); 
 }

Just because Inheritance defined in this way only, So you cant override method. Concept of super class is to put the general behavior at super level type. And your sub class is of the same type of that super class. And if you need some specific behavior in sub class then override super class method and change it according sub class requirement.

If you consider inheritance - its primary purpose is to allow child classes to add upon the parent class' attributes and behaviour. Besides adding on, or extending, the child class might also choose to override. Since overriding doesn't make much sense for attributes, it's largely used for methods. Also note, when you override methods, you pretty much re-write the entire logic, or, invoke super.method() and then do something after.

Now, if you were to try and apply the same concept to methods - you'll see that it really doesn't make much sense in case of an object oriented language. Methods always encapsulate behaviour only. Attributes are not associated with methods. So by extending, you'd only either end up re-writing the entire logic, or, do something along the lines of super.method() .

However, all this makes sense in case of a functional programming language like Haskell, Scala or Javascript. This is because these languages consider functions are first class members - meaning, you can write your program using only functions, without any classes. In such languages, you'll find that function inheritance is available in some or the other form.

There is no way you can extend a method; it's against OOP concepts.

You can implement the functionality in the following two ways -

public static void OldMade()
{
    Connect("+1800PIZZA");        
    Say("MYID:69"); 
    Request("PIZZACITROEN_BLACK");
}

public static void MadeInIndia extends this.OldMade
{
    OldMade();
    Send("CorrectionPandit"); 
}

private static void main(Strig args[])
{
    MadeInIndia();
}

Or

public static void OldMade()
{
    Connect("+1800PIZZA");        
    Say("MYID:69"); 
    Request("PIZZACITROEN_BLACK");
}

/*public static void MadeInIndia extends this.OldMade
{
    OldMade();
    Send("CorrectionPandit"); 
}*/

private static void main(Strig args[])
{
    //MadeInIndia();
    OldMade();
    Send("CorrectionPandit"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM