Category Archives: Java Class

Classes In Java


Classes In Java

Classes in Java are the template or blue print form which the objects are actually made.

All code written in java is inside class. We can create our own classes in Java. These classes describe the objects of the problem domains of our application. Our own classes are essential to adapt the classes that are supplied by the standard library to our own purposes. Class can be built on other classes by extending it. Java comes with a “Cosmic Superclass” called Object. Object is the factory for all object. All other classes extend this. When we extend a existing class, the new class has all the properties and methods of the class that we extend.

Standard naming convention for Class:

  • Start with letter, followed by any combination of letter and digits.
    Class names are nouns that start with an uppercase letter in each of the words.CamelCase
    File name for the source code must be same as the name of the public class, with the extension .java.
  • Data in an object are called its -> instance fields.
    Procedures that operate on the data -> methods.

    A specific object that is an instance of a class will have specific values for its instance fields. The set of these values is the current state of the object. Whenever you invoke a method on an object, its state may change. Objects that are instance of the same class, share a family resemblance by supporting the same behaviour. The behaviour of an object is defined by the methods you can call.

    How to identify classes?
    First find classes, then add methods to each class. Classes are the nouns in problem analysis. Methods are verbs in problem analysis.

    Relationship between classes:
    Dependence(“uses a”): Dependence or “uses a” relationship is the most obvious type of relationships. e.g.,
    Inheritance(“is a”): Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word.
    is a --- House is a Building
    class Building {
    .......
    }
    class House extends Building {
    .........
    }

    Aggregation(“has a”): Such relationships are usage based. It shows whether a class consist of HAS-A, certain thing. Code duplication and bugs are reduced by this relationship.
    For example:
    Java Code:
    public class Vehicle{}
    public class Speed{}
    public class Van extends Vehicle{
    private Speed sp;
    }

    Class Van HAS-A speed is shown. Speed’s separate class doesn’t require putting the complete code which has relation with speed, inside the Van class, that makes the reusage of speed class possible in various applications.

    Class Design hints in Java:
    Here are some hints to make Java Classes more acceptable in well-mannered OOP circles.

    1. Always keep data private.

    Datas are kept private to comply with encapsulation. This rule is followed to ensure that though the data representation may change, how they are used will change much less frequently. When data are kept private, changes in their representation do not affect the user of the class and bugs are easier to detect.

      Always initialize data.

    Java dont initialize local variables, but it does initialize instance fields of objects.

      Dont use too many basic types in classes.

    The idea is to replace multiple related uses of basic types with other classes. These keeps classes easier to understand and change.

      Not all fields need individual field accessor and mutators.
      Use a standard form for class definitions.
      Break up classes with too many responsibilities.
      Make the name of our classes and methods reflect their responsibilities.
  • Hence, this is my brief understanding on Classes In Java

    Advertisements