Thursday, August 27, 2015

Basic Java for OAF

 

OOPS Concepts

Object:-

An object is a software bundle of related state and behavior.

Software objects are often used to model the real-world objects that you find in everyday life.

Real-world objects share two characteristics: They all have state and behavior.

Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior.

 An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.


Class:-

            A class is the blueprint from which individual objects are created.
           
            Public class Dog
{
                        String breed;
                        int age;
                        String color;
                        Public void barking ()
{
              //write the required code
                         }
  
                       
Public void hungry ()
{
-----
-----
                         }
}
Abstraction:-

Hiding unnecessary data from user is called Abstraction access   specifiers like public, private etc are used to provide different level in abstraction.

Example:-

Suppose take a car: In that we will have breaks and staring and etc...   Here when we are changing the gear box to up or bottom then gears will work but we don't know how they are managing internally and we don't no how it is working. As a driver we don't need to know how it is working internally when we are changing every time. That is the reason they will hide the wires and cables internally and they will show up only gear box to us.

Advantages:- Code will be clean and for managing it will be very easy
Encapsulation:-

Taking data and object in a single unity is called Encapsulation.
A class is example for Encapsulation. In our class we need to make all our variables and methods keeping together.

Advantage:- Maintenance will be good.
 Inheritance:- 

Creating a new class from existing class is called Inheritance.
acquiring the properties from super class to subclass.

Example:-

Suppose take two java classes Class A contains 2 variables and Class B Extends Class A that means here Class B can access the class A variables without declaring it. If we do this then the memory will be less and reusable.

class B extends A
 {
    // new fields and methods defining
    // Class A methods are inherited here.
}
Reusability is main advantage in inheritance.
 Interface:-

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface.
Methods form the object's interface with the outside world. In its most common form, an interface is a group of related methods with empty bodies.

Example:-

The buttons on the front of your television set are the interface   between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.
 Package:-

A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
Polymorphism:- 

If single method performs different task is called polymorphism.
There are 2 types:
1) Compile time polymorphism
.
2) Run time polymorphism
.

Method overloading is an example for Compile time polymorphism and Method overriding is an example for Run time polymorphism.
Overloading:-

If you have two methods with same name in one Java class with different method signature than its called overloaded method in Java.
Overriding:-

Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that function.

No comments:

Post a Comment