Powered by Blogger.

Friday 18 December 2015

OOP(Object-Oriented Programming )


OOP(Object-Oriented Programming ) are used three Principals That Design Oops Module.
  •   Encapsulation.
  •   Inheritance.
  •   Polymorphism.
 An Introduction:
One of the most fundamental concept of OOPs is Abstraction. Abstraction is a powerful methodology to manage complex system. Abstraction is managed by well-defined objects and their hierarchical classification. for example a car in itself is a well-defined object, which is composed of several other Smaller objects like a gearing system, steering mechanism, engine which are again have their own subsystems but for humans car is one single object, which can be managed by the help its subsystems, even if their inner details are unknown. Java is an object-oriented language because it provides the features to implement an object oriented model. These features includes encapsulation, inheritance & polymophism. oops is about developing an application around its data, e.g. objects which provides & the possible oprations in their own way.
Principles of OOPs:
1. Encapsulation:
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Out Put:
 User : James Age : 20 
2. Inheritance:
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also. Inheritance represents the IS-A relationship, also known as parent-child relationship.
Why use inheritance in java 
  • Method Overriding (so run time polymorphism can be achieved).
  • Code Re-usability. Syntax of Java Inheritance.



The extends keyword indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass. Understanding the simple example of inheritance inheritance in java As displayed in the above figure, Programmer is the subclass and Employee is the super class. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee.
 
Out Put:
 Programmer salary is:40000.0 
 Bonus of programmer is:10000. 
3. Polymorphism:
Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms. There are two types of polymorphism in java: compile time polymorphism and run time polymorphism. We can perform polymorphism in java by method overloading and method overriding.If you overload static method in java, it is the example of compile time polymorphism. Here, we will focus on run-time polymorphism in java. Run-time Polymorphism in Java Run-time polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at run-time rather than compile-time. In this process, an overridden method is called through the reference variable of a super-class. The determination of the method to be called is based on the object being referred to by the reference variable. Let's first understand the up-casting before Run time Polymorphism.
Up-casting 
When reference variable of Parent class refers to the object of Child class, it is known as up-casting. For example:

A a=new B(); //up-casting

Example of Java Run-time Polymorphism
In this example, we are creating two classes Bike and Splendor. Splendor class extends Bike class and overrides its run() method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at run-time. Since method invocation is determined by the JVM not compiler, it is known as run-time polymorphism.
 

Out Put:
 running safely with 60km.