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

3 comments:

  1. Hi, Nice description about Oops concepts in java.Thanks, its really helped me......

    -Aparna
    Theosoft

    ReplyDelete
    Replies
    1. Thank You for your comment Aparna...I will now continue to post new updates on this blog.

      Delete
  2. Hi, I desire to subscribe for this web site to obtain most up-to-date updates, so where can i do it please help.

    Curso java

    ReplyDelete