Results 1 to 10 of 10

Thread: oops

  1. #1

    oops

    hi,plz give a simple real time eg. for overriding&overloading


  2. #2

    oops

    Overriding method definitions
    In a derived class, if you include a method definition that has the same name and exactly the same number and types of parameters as a method already defined in the base class, this new definition replaces the old definition of the method.

    Explanation
    A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the methods defined in the superclass. This is referred to as method overriding. The following example demonstrates method overriding.

    Step 1
    In this example we will define a base class called Circle

    class Circle {

    //declaring the instance variable
    protected double radius;

    public Circle(double radius) {
    this.radius = radius;
    }

    // other method definitions here

    public double getArea() {
    return Math.PI*radius*radius;
    }//this method returns the area of the circle

    }// end of class circle

    When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.

    Step 2
    The next step is to define a subclass to override the getArea() method in the Circle class. The derived class will be the Cylinder class. The getArea() method in the Circle class computes the area of a circle, while the getArea method in the Cylinder class computes the surface area of a cylinder.

    The Cylinder class is defined below.

    class Cylinder extends Circle {

    //declaring the instance variable
    protected double length;

    public Cylinder(double radius, double length) {
    super(radius);
    this.length = length;
    }

    // other method definitions here

    public double getArea() { // method overriden here
    return 2*super.getArea()+2*Math.PI*radius*length;
    }//this method returns the cylinder surface area

    }// end of class Cylinder

    When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass(Circle).

    Example code
    This is the code to instantiate the above two classes

    Circle myCircle;
    myCircle = new Circle(1.20);

    Cylinder myCylinder;
    myCylinder = new Cylinder(1.20,2.50);

    Please tell me if i make this clear...even you can refer to bala guruswami for java

    Devising unique naming conventions can be a tedious chore, but reusing method names via overloading can make the task easier. Here's a look at how this technique works in Java.
    Naming conventions are an important aspect of any development project, but coming up with unique names can be somewhat tedious. One way to simplify the chore is to reuse method names via overloading. Overloading is the ability to have a class that has multiple methods with the same name that are differentiated by the number and type of arguments passed into the method.

    What's in a name?
    When assigning names to classes, methods, and member variables, it is important to use names that are easy to understand. For example, creating a class for defining a person would lead to an appropriate class name of Person. Calling it a gibberish name like dkjfb would be valid Java code but absolutely nonsense to any programmer working on the system in the future. The Person class has the following designation:
    class Person {
    private String firstName;
    private String lastName;
    }

    The code listing declares a class called Person, with two member variables to store first and last names. The names assigned to the member variables are analogous to their use, so it is easy to identify what type of data is stored in them. When developing the Person class, we need to populate these member variables when the object is created.

    Object construction
    Creating a new object instance triggers the class's constructor method. The following code utilises a basic constructor that accepts no arguments:
    class Person {
    private String firstName;
    private String lastName;
    Person() {
    this.firstName = "";
    this.lastName = "";
    } }

    The basic constructor initialises the member variables with empty strings. Upon further investigation, it is discovered that often the objects are created with known names but sometimes only the last name. You can use method overloading to create multiple method versions, but each has its own method signature. The signature defines what parameters are accepted by the method. For example, here's the method signature for the previous constructor:
    Person()

    This method can be overloaded to accommodate first and last names or only the last name:
    class Person {
    private String firstName;
    private String lastName;
    Person() {
    this.firstName = "";
    this.lastName = "";
    }
    Person(String lname) {
    this.firstName = "";
    this.lastName = lname;
    }
    Person(String fname, String lname) {
    this.firstName = fname;
    this.lastName = lname;
    } }

    Any two methods with the same name in a class must have different parameter types or a different parameter count; if not, the compiler will reject them. The class may now be declared as follows:
    Person p1 = new Person();
    Person p2 = new Person("Patton");
    Person p3 = new Person("Patton", "Tony");

    A current Java feature
    Overloading is used in the standard Java classes. The System.out.println method accepts multiple parameter lists. This sample code snippet is valid:
    System.out.println("Builder.com");

    as is this one:
    int test = 2;
    System.out.println(test);

    Both code snippets compile and execute with no problems. The println method has been designed to accept varying parameters, so overloading is applicable beyond constructors. To further illustrate this, we'll extend our sample class by adding a print method to output the first and last names:
    class Person {
    private String firstName;
    private String lastName;
    Person() {
    this.firstName = "";
    this.lastName = "";
    }
    Person(String lname) {
    this.firstName = "";
    this.lastName = lname;
    }
    Person(String fname, String lname) {
    this.firstName = fname;
    this.lastName = lname;
    }


    public void Print() {
    System.out.println(firstName + " " + lastName);
    }

    public void Print(String pout) {
    System.out.println(pout + " " + firstName + " " + lastName);
    }

    }

    These two print methods output the member variables, with one method accepting text to prepend to the output and the other not.

    When to use overloading
    Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks. This approach will only confuse other developers who get a peek at your code.

    Let me know if you need any more clarification regarding this


  3. #3
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: oops

    Some of the threads are merged for better management of the site.

    MODERATOR


  4. #4
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    Re: oops

    Overriding means method in the subclass with the same method name &same signatures of a method which is already exists in its parent class..
    for eg:a child who is a xeroxcopy of his/her parent inherits all the behaviour of his/her parent..but he has his own behaviour too..

    overloading means more than one method with the same name with in the same class..it has 2 conditions...
    1) if return type of method is same than the number of arguements must be different..or..return type of the arguements must be differ if it contain same number of arguements of already existing method...
    2) if return type of the arguements are same with same number of arguements then return type of method should be differ from already existing method..
    for eg:identical twins with different behaviour..


  5. #5
    Junior Member
    Join Date
    Jul 2007
    Answers
    4

    Re: oops

    Quote Originally Posted by amaravadi.krishna81 View Post
    hi,plz give a simple real time eg. for overriding&overloading
    //overriding
    class example
    {
    public:
    getdata();
    };
    class driveublic example
    {
    public :
    getdata()
    {
    example:getdata();
    }
    };
    //overloadding
    class example
    {
    public:
    example()
    {
    }
    example(int,int);
    };


  6. #6
    Junior Member
    Join Date
    Jul 2007
    Answers
    4

    Re: oops

    Quote Originally Posted by nameetapani View Post
    //overriding
    class example
    {
    public:
    getdata();
    };
    class driveublic example
    {
    public :
    getdata()
    {
    example:getdata();
    }
    };
    //overloadding
    class example
    {
    public:
    example()
    {
    }
    example(int,int);
    };
    opps sorry actually i didnt read the question properly.


  7. #7
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: oops

    dear
    nameetapani

    please take care not to repeat the same mistake again.


  8. #8
    Junior Member
    Join Date
    Sep 2006
    Answers
    9

    Re: oops

    Krishna,
    Assume in a banking application you are having a business bean which handles withdrawls from Savings,current and number of other related accounts... you can map this as overloading.
    In a manufacturing unit, there is a common structure to be implemented to a like-list-of-products, but each product again differs in its own aspects.
    I think this makes sense to your question, and is it not the real time example


  9. #9
    Junior Member
    Join Date
    Feb 2007
    Answers
    7

    Smile Re: oops

    overriding is concept which undertakes in inheritance, its used by the keyword virtual. when virtual keyword is used before a function that can be redefined anywhere where it is inherited. if virtual keyword is not used even if it is redefined the compiler will give preference to the base class function.

    overloading is noting but it is same class when we use same function names with different signatures.

    note overriding should be the function with same signatures


  10. #10
    Contributing Member
    Join Date
    Oct 2008
    Answers
    72

    Re: oops

    Interface generally refers to an abstraction that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide multiple abstractions of itself. It may also provide a means of translation between entities which do not speak the same language, such as between a human and a computer. Because interfaces are a form of indirection, some additional overhead is incurred versus direct communication.

    The interface between a human and a computer is called a user interface. Interfaces between hardware components are physical interfaces. This article deals with software interfaces, which exist between separate software components and provide a programmatic mechanism by which these components can communicate.

    Interfaces in practice

    A piece of 'software' provides access to computer resources (such as memory, CPU, storage, etc.) by its underlying computer system; the availability of these resources to other software can have major ramifications -- sometimes disastrous ones -- for its functionality and stability. A key principle of design is to prohibit access to all resources by default, allowing access only through well-defined entry points, i.e. interfaces.[citation needed]

    The types of access that interfaces provide between software components can include: constants, data types, types of procedures, exception specifications and method signatures. In some instances, it may be useful to define variables as part of the interface. It often also specifies the functionality of those procedures and methods, either by comments or (in some experimental languages) by formal logical assertions.

    The interface of a software module A is deliberately kept separate from the implementation of that module. The latter contains the actual code of the procedures and methods described in the interface, as well as other "private" variables, procedures, etc.. Any other software module B (which can be referred to as a client to A) that interacts with A is forced to do so only through the interface. One practical advantage of this arrangement is that replacing the implementation of A by another one that meets the same specifications of the interface should not cause B to fail — as long as its use of A complies with the specifications of the interface


  11.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact