Friday, April 19, 2013

Java concept and discussion

Here is the examples of oops concept -

in addtiion to my previous post about the oops concept definitions in this post i will try to explain practically with the help of code, please read and let me know your feedback or any comment.

Iniheritence - As i already explained in my previous post  basic oops concept  the basic definition of iniheritence, In this post i will show you code example -


package test.com.oops;

// Parent class which describe basic feature of parents.
public class ParentClass {

      // Properties of parent 
private String name;
private static String bodyStructure;
private  String age;
private String behaviour;

// Base method of parent which describe how parents thinking capability build.
public void ThinkingCapability()
{
}

// Describe parent normal body structure i.e. 2 leg , 2 eyes, 2 hand etc.
public String ParentBodyStructure()
{
return bodyStructure;
}

// Parents walking feature here
public void Walking()
{
}
// Parents talking feature described here
public void Talking()
{
}}

Child class example - 


package test.com.oops;

// Child class which inherit the properties of parent class
// Child has automatically inherit certain properties from parent like thinking,walking,talking.
public class ChildClass extends ParentClass {

// Child properties
private String childBodyStructure;

//Inherit from parent class by overriding parent class.
public void ThinkingCapability()
{
// child inherit this properties from parent automatically
// Can have additional capabilities of thinking 
}

// Inherit properties from parent class by overriding parent method.
public String ParentBodyStructure()
{
        // Child has the same body structure as parent
//In some feature it can be differ like height, color etc.

return childBodyStructure;
}}


Encapsulation - binding of class and its variable into single unit is called encapsulation -

Code example of encapsulation -


package test.com.oops;

// A class with private fields and public methods.
public class EncapsulateClass {

// Instance variable are made private so none other class can change the value.
private static  String name;
private static int age;

// Getter and Setters are public method which use for accessing the value of class variable
public static void setName(String name) {
EncapsulateClass.name = name;
}
public static String getName() {
return name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
EncapsulateClass.age = age;
} }




Abstraction - This concept can be achieved by abstract class and interface in java i have explained this logic with the help of abstract class -

package test.com.oops;
// Abstract class with abstract method 
public abstract class Account {
// We have made this function abstract since withdraw functionality will be same for all type 
// of account whether it is saving or current account.
public abstract void WithdrawAmount();

    // concrete method with some implementation
    // this method can be override in child class and logic can be change based on account type. 
public void CalculateInterest(double principleAmount)
{
    double   principal = 0;
    float intRate = 0;
    int numOfYears = 0;
    
double Interest=(principal * intRate * numOfYears) /100;
}}

Child class as saving account class for encapsulation -

package test.com.oops;
// child class which override the feature of base class
public class SavingAccount extends Account {

// Override this method since it is abstract
// every account has the feature of withdraw
@Override
public void WithdrawAmount() {
}}

Polymorphism - As explained this can be achieved by method overloading and overriding  in java in my previous blog.

please see below examples -

Method overloading -

package test.com.oops;

// This is base class
public class PolyMorphismTest {
// This is base method
public int add(int i,int j)
{
int k;
return k=i+j;
}
// Overloading of add method 
// This is called static binding or compile time polymorphism
public double add(int i,int j,float k)
{
double l;
return l=i+j+k;
}
// Parent class method
public void TestOverrideMethod()
{
System.out.println("IN parent class");
}}


Method overriding -

package test.com.oops;
// This is child class 
public class PolyMorphismTestChild {
// Override the method - This is called run time polymorphism.
// PolyMorphismTest obj=new PolyMorphismTestChild();
// This can be known and run time only - obj.TestOverrideMethod();
public void TestOverrideMethod()
{
System.out.println("IN child class");
}}


Please read the above examples and let me know your views .

Thanks
Ashish

Thursday, April 11, 2013

Java basic by Ashish -

This is my first blog on java concept, I am just sharing an overview of the basic definition of java and there concept. More details and example of every concept will be shared in next post.

 Please read and share your comment/feedback, if anything is not correct please feel free to share it.

"Sharing knowledge is the best way of improvements"





Java is a platform independent language -
Here one questions comes in my mind what is platform in programming environment ?  Platform is nothing but an system where we can compile and run our program source code. i.e windows operating system , UNIX operating system and so on.

Java is platform independent because java source code compiled in windows system can be executed in UNIX based system, there is no need of compiling the code again in new environments  JVM.
So java is also called as "write once run anywhere".

Java is object oriented language -

Java programming  is built upon object orientation -


OBJECT is nothing but any real world thing which has the attributes, behavior and states, i.e. car,bike,bicycle

CLASS in java is the template of similar objects or you can say it represent the states and behavior of objects. like car is class of similar objects (maruti 800,i 10, i 20 etc are type of car object).

Java has below main OOP concept  -

 Inheritance

Inheriting the properties of parent class in child class is called Inheritance, in Java inheritance is primarily introduce for  Code Re usability.
I.e. Shape is parent class and it can contain the draw function which has the implementation of how any shape can be draw.

Now square,rectangle can extend the shape class and reuse the draw function code to implement their own drawing logic.

Here the child classes inheriting the properties of parent class Shape.

Encapsulation
Hiding the implementation details from the outer world is called encapsulation , in more details biding of data members and methods into a single unit is called encapsulation .

Encapsulation can be achieved by making instance variable as private and methods as a public.

encapsulation will ensure that no other class will able to change your implementation logic, they can just use the functionality as it is.

Abstraction - 
Abstraction in java can be achieved by interface and abstract class, don't go in details for interface and abstract class as of now i will cover these concept in my next blog with example.

Abstraction in terms of java programming refers to showing important aspect of program to other program.
 

For example - TV remote is use for controlling the channels it doesn't show how its internal functionality is working to change the channel.

TV remote making structure is called encapsulation (i. e. Binding of data and its method together) and its button panel is called abstraction (showing number on buttons and its functionality is abstract for user.).
 

Polymorphism -  
Polymorphism in java refers to many forms of single object for example an actor can be associate with different role like he can do role of father , he can do role of son and so on.

In Java polymorphism can be achieved by 2 ways -

Method overloading - Method overloading in which 2 method inside same class with same name should have different parameter, return type is not mandatory to be change. it can be achieved  in single class.

Method overriding - Method name and parameter should be same, implementation needs to be change in derived class. it can be achieved in 2 classes.

Practical example and source code of each and every concept will be shared in next post, be in touch .

Thanks.