What are Classes in Java?

In the real life, you’ll often find many personal things all of the same kind. There may be countless numbers of other bikes existing, all of the same design. Each bike was come up with same set of designs and therefore contains the same elements. In object-oriented conditions, we say that your bike is an type of the type of things known as bikes. A class is the strategy from which personal things are designed.

The following Bicycle class is one possible execution of a bicycle:

java classes

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:"
+
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

The format of the Java development terminology will look new to you, but the design and style of these types is based on the first conversation of bike things. The areas cadence, speed, and gear signify the object’s state, and particularly (changeCadence, changeGear, speedUp etc.) determine its connections with the actual.

You may have observed that the Bicycle class does not contain a main method. That’s because it’s not a complete application; it’s just the strategy for bikes that might be used in a software. Down to making and using new Bicycle things connected to some other category in your program.

Here’s a BicycleDemo category that produces two individual Bicycle things and makes their methods:

class BicycleDemo {
    public static void main(String[] args) {

        // Create two different 
        // Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on 
        // those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:

cadence:50 speed:10 gear:2
cadence:40 speed:20
gear:3

In object-oriented development, a category is an extensible program-code-template for developing things, offering initial principles for state (member variables) and implementations of actions (member features or methods). In many different languages, the course name is used as the name for the course (the design itself), the name for the standard constructor of the course (a subroutine that produces objects), and as the type of things produced by instantiating the class; these unique ideas are easily conflated.

When an object is created by a constructor of the course, the causing item is known as an type of the course, and the participant factors specific to the item are known as example factors, to comparison with the course factors distributed across the course.

Don't be shellfish...Email this to someonePrint this pageTweet about this on TwitterShare on StumbleUponShare on TumblrShare on LinkedInShare on RedditDigg thisShare on Google+Pin on PinterestShare on FacebookBuffer this page

One thought on “What are Classes in Java?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>