Thursday 23 February 2017

Java Training in Chennai - Introduction to Object Oriented Training

Introduction to Object Oriented Programming – Java basics

Hidden implementation or Abstraction

 The solution to a problem is now divided into class creators and client programmers. Client programmers will collect the classes created by the class creators to develop an application. Now the aim of a class creator will be to build a class that will expose only the necessary information for the client programmer and make the rest of the information will not be allowed to be accessed by anyone.  This process of hiding the information is called as Abstraction. But why are we doing this abstraction?
There are two important reasons for this abstraction.
1.       By hiding the implementation of the methods and not allowing the client to access this, the class creators can change the implementation whenever necessary without worrying about the impact that will cause on the client programmer.
2.       By hiding the tender portion of the object from the client programmer, we are avoiding the corruption of the tender portion by the client programmers by mistake or by carelessness.
Now how this is made possible in Java? Java has three keywords public, private, and protected. These are called as access specifiers which determine the visibility of the code.
  • .       Private – No one can access the elements or methods except the class creator or the methods inside the class. Anyone outside the class cannot access these elements or methods. Private is a wall constructed between the methods in the class mentioned as private and the client programmers.
  •         Protected – It acts like a private with an exception that the inheriting class can access the protected members but not the private members.  Inheritance will be introduced to you shortly.
  •         Public – The elements following the public is accessible by everyone.
The default, which comes by default if you don’t provide any of the specifiers in Java is Private. This private is also called as package access in Java because the members that are private in a class is accessible by other members of the class in the same package but not by the classes outside the package. 


    Reusing the implementation or Inheritance

Reusing a code is one of the biggest advantage of the object-oriented programming language provides. The simplest way of reusing a class is to directly place an object of the class inside a new class.  Thus we can create a new class with number of objects of different type. This way of creating a new class from the existing classes is called as composition and this is referred as has-a relationship. For example, “A book has pages” where book is a new class that contains any number of objects of class page.  Another example, “A car has an engine” where car is a new class that contains an object of the class engine. In composition, the objects of the existing classes will always be private in the new class making it inaccessible for client programmers directly.
The other way of reusing the implementation is through inheritance.  We undergo a lot of trouble while we create a class and create a new class that looks similar in the functionality. Will it not be nicer when you can clone an existing class and add or modify the cloned class to create a new class?  The class that from which we are cloning a new class is called as base class or super class or parent class. The new class that is cloned is called the derived class or inherited class or subclass or child class.  The methods that are part of the public and protected access specifiers will be cloned to or inherited to the child class from the parent class. Also we can add new methods or modify the existing method in the child class.

The best example we can take is shape. Circle, Square, and triangle are shapes which will have a size, color, position, edges, and lot more. Also we can draw, erase, move and color the shape. Circle, square, and triangle will also have a size, color, position, edges, and also we can draw, erase, move, and color these. Thus Shape is the base class and circle, square, and triangle will be inherited from the shape. This way OOP provides the advantage of code reuse without allowing you to re-write everything from the base.

Polymorphism

There’s a problem that we have to deal in inheritance. The problem is we attempt to make the derived object type as their generic base type. For example circle as a shape, eagle as a bird, car as a vehicle. I want to draw a circle but because of inheritance it will draw a generic shape. So the compiler doesn’t know at compile time which piece of code will be executed. So when we send a message, the draw method of the circle, or square, or triangle can be executed and thus the proper code is executed based on the specific object type. For example, if the object is circle then the draw method in the circle will be executed.

One of the major difference between the non-OOP compiler and the OOP compiler is the way binding happens. In non-OOP compiler, early binding happens. Here during the compilation, the compiler generates a call to the function name and at run time the call will resolved to the address of the code that needs to be executed. But in OOP compiler, you send a message to the object, the code that will executed for this message will not be known until run time. The work of compiler in this case is just to verify if that method we are calling exists for that particular object and checks the type of the arguments and the return value.

Java has a special code called absolute call to perform this late binding and this code does all the calculation of the address for the method with the help of the information present in the object. Thus, objects will behave differently based on the content of the special code. This way of doing late binding is called as polymorphism. The exact meaning of polymorphism is the same function will behave differently based on the type of the object it is called.

In C++, we use the keyword virtual to say that the particular method is having the late binding property but in Java, by default, the behavior of the method is dynamic binding or late binding and rules out the need for us to remember and add the keyword for making a specific method to have late binding flexibility.

Encapsulation

Encapsulation is about wrapping the data and the methods that will act on these data into a single unit. Encapsulation is made possible through abstract data type called class. To know about classes and object, please click here

The basics of Java is covered in this topic and for learning Java in-depth from the best tutors in the industry, join the best Java training in Chennai. To know more about us, visit us on College to Corporate