Menu
College to Corporate
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.
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. This is just a basic introduction to Java, to learn in-depth from the best tutors in the industry, through our best Java training in Chennai. College to Corporate is the best IT training institute in Chennai.
0 Comments
Leave a Reply. |
Contact us:
|