View RSS Feed


Object-oriented programming tutorials in Java.

  1. Returning a Class or Interface

    by , 04-26-2012 at 05:39 PM
    When a class name is used by a method as its return type, like whosFastest would be doing, typeís class of returned objects shall be subclass or exact class of return type. Letís say that class hierarchy is there where java.lang.Number has a subclass ImaginaryNumber that is also the Objectís subclass that has been illustrated in figure given below:

    Name:  1.jpg
Views: 315
Size:  3.1 KB

    ImaginaryNumberís class hierarchy

    Letís say a method has been declared so that ...
  2. Returning a Value from a Method

    by , 04-26-2012 at 05:36 PM
    To the code, a method is returned which invokes it, when:

    • In method, all statements are completed.
    • reaches to the a return statement or
    • an exception is thrown (covered later)

    Which ever would come first?

    Return type of the method is declared in the method declaration. Within methodís body, return statement is used so that to return value.

    Method which has been declared void will not return value. Return statement is not need ...
  3. What Is a Package?

    by , 04-26-2012 at 05:34 PM
    Package is basically the namespace which does the organization of related interfaces & classes. Conceptually, one might think regarding packages which are same to those folders which are present at your computer. HTML pages could be placed in 1 folder, images in some other folder & applications and scripts in another one. As software which has been written in Java might be composed of thousands of classes, things could be organized by placement of the related interfaces & classes into ...
  4. What Is an Interface?

    by , 04-26-2012 at 05:33 PM
    As it has been discussed, objects show the interaction which takes place with outer world via certain methods being exposed by them. Interface of object is formed by the methods, with outer world; buttons which are present at television set are the interface b/w electrical wiring and you. Power button is pressed to turn it off or on.

    Its common form shows that interface is related methodís group along with empty bodies. Behavior of cycle which is being specifief to be an interface ...
  5. What Is a Class?

    by , 04-26-2012 at 05:31 PM
    Various individual objects are present in real world which are of same kind. Hundreds or even thousands of bicycles might be present, of same model or make. Every bicycle was made by similar blueprints sets and hence consists of similar components. In terms of object-oriented, we shall say that bicycle is class of objectís instance, known as bicycle. Class is considered to be the blueprint which creates the individual objects.

    One possible bicycle implementation of bicycle class is ...
  6. What Is an Object?

    by , 04-26-2012 at 05:30 PM
    Key to understand the objects oriented technology are objects. Look around & many examples would be present of real world objects, for example your desk, your bicycle, your dog, your television set etc.

    Two characteristics are present in real world objects. Behavior and state are present in all of them. Dogs have behavior (fetching, barking, wagging tail) & state (breed,hungry, color,name). Bicycles also consist of behavior (applying breaks, changing gears, changing pedal ...
  7. Using Inheritance - Substitution vs. Extension

    by , 11-30-2011 at 03:51 AM
    We are going to look at inheritance hierarchy. If we take a classic approach, as shown below we would see the methods that have been defined in the base class overridden in the derived classes. Inheritance guarantees that any derived class will have the interface of the base class. If you follow this diagram below, derived classes will also have no more than the base-class interface.

    Name:  Inheritance-Example.jpg
Views: 1036
Size:  16.7 KB

    This can be thought of as pure substitution, because ...
  8. Design Principle: Choose Composition vs. Inheritance

    by , 11-30-2011 at 03:48 AM
    With composition or inheritance you can place sub-objects inside your new class. If you do this explicitly than you are using composition otherwise if you do this implicitly, you are using inheritance. I will outline the difference between the two below as well as point out one of the key design patterns to use in your development. Prefer composition vs inheritance.

    Composition is used when you want the features of an existing class to be included inside your new class, but not be ...
  9. Using Name Hiding for Overriding

    by , 11-30-2011 at 03:45 AM
    Sometimes you will have a java base class that has a method thatís been overloaded several times. It is important for the programmer to remember that redefining the method name in the derived class does not hide any of the base-class versions. Therefore you should keep in mind that overloading works irrespective of whether the method has been defined at this level or in a base class:

    Java Code:
    import static com.acme.examples.Print.*;
    public class Mother {
    	char foo(char
  10. Composition Syntax for Reuse

    by , 11-29-2011 at 06:21 PM
    One of the best features of Java is code reuse. But like any tool, it needs to be used effectively. Most programmers might be lead to think that when I talk of reuse only relates to extending a class. Now there is nothing wrong with reusing a class by extending it or using it directly. This type of reuse is using the class without the risk changing the existing code. First we can create objects of the existing class inside the new class. We call this composition. It is a new class composed of objects ...
  11. Composition Rather than Inheritance

    by , 11-29-2011 at 02:27 AM
    One of the more abused features of object oriented languages is inheritance. Second on the list of abused features of Java as an outgrowth of inheritance is the use of polymorphism. Developers seem to believe that everything should be inherited, and that classes should use polymorphism for every object irrespective of whether the arguments will be provided from external sources. Doing these sorts of things will make your designs more difficult to understand and lead to complications later when ...
Page 2 of 2 FirstFirst 12