Category Archives: Java

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


    JAVA -At its very beginning……..


    Java is a simple, object oriented, distributed, robust, secure, portable, interpreted, high performance, multithreaded and dynamic language.

    A group of Sun Engineers, led by Patrick Naughton and James Gosling designed a small computer language that could be used for consumer devices like cable TV switchboxes. These devices used low power, low memory and supported multiple architecture. This project was named as “Green”.
    A portable language that genereated intermediate code for a hypothetical machine( called Virtual Machine), was designed to fulfill the requirement for small, tight and platform neutral code. These intermediate code could then be used on any machine that had the correct interpreter.
    Green project engineers used a virtual machine so as to solve their problem.
    Sun people behind Green project came from a UNIX background, so they made the language Object Oriented rather than the Procedural language. Gosling decided to call his language “Oak”. The people at the Sun later realized that Oask was the name of an existing language. So, they changed the name to Java.
    Java had some misconceptions in the early days. It was thought that :

      Java is an extension of HTML,
      Java is an easy programming language to learn,
      Java is an easy environment in whichto program,
      Java will become a universal programming language for all platforms,
      Java is just another programming language,
      JavaScript is a simpler version of Java.