Share your knowledge

Friday 6 January 2017

Java Basics to work with Android Projects

Java Basics:
  
Class: Class is a blueprint / template to create an object.

Object: Object is a real world thing, it can be anything ex: phone, file, bottle, bag, dog etc.

Which is first class or object?

Ans: Many people will think that class is first as objects are created from class but class will be created once we imagine an object so object is first in my point of view.

Why should i create a class/ object: Well, Programming languages are used to simplify or beautify our life by making the complex problems simpler. Suppose you have a laundry shop where people were used to come to get their cloths washed and iron.

Now, you want to maintain the book with list of people who come to your laundry shop. which may looks like below.




This record contains 6 columns/fields Name, No. of Shirts, No. of Pants, Advance paid, Laundry completed?, Address and Delivered?

Name: Name of the people who come for laundering
No. of Shirts: Number of shirts given for laundering.
No. of Pants: Number of pants given for laundering.

Total Amount: Total amount for laundering
Advance Paid: Advance paid
Laundry completed: Is the laundering completed or not?
Address: Delivery address
Delivered: Is the delivery completed or not?

Disadvantages of maintaining a book like above:

1. You may lost your book at any time.
2. Becomes complex in calculating total amount when number of shirts and pants were more
3. Becomes difficult to modify the values in case of address change etc.
4. Maintaining large number of people need lot of books and in turn paper wastage.


To overcome all these problems programming languages were introduced which makes complex problems simpler.

Our next goal is to tell the computer to do all these things(Maintaining a book like above). Unfortunately computer doesn't understands human language so there should be someone who can make computer understand these, he is non other than a programming language.

Let's consider that programming language as Java.

As we heard lot of times that Java is object oriented programming language. 

What is object oriented programming language?: 

Ans: A language which deals with objects( real world entities ) is called Object Oriented Programming language(OOP).

Here in our case Object(real world thing) is a User who comes for the laundry.

Let's create a template(class) which describes this object(entity)
here is
Creating class:
Syntax:

 // ClassName is the name of the class, you can give any name based on your //need

class ClassName{

//it may contain some properties(fields)
//it may contain some functionality(methods)

}

Declaring Variables:

DataType: Name itself suggests that it is "type of data we are working with", it can be an integer type, string type, floating value type or anything.

Ex: int , String, float, double etc...

Declaring varibles/properties:

int amount;

String name;

String address;

Note: If you are very beginner in java and if you are getting confused then go through some other tutorials to learn basics of java first.

Declaring Methods/Function:

Method: Methods are written do some functionality, for example it may be used to calculate total amount to be paid for 2 shirts and 3 pants.

Method Declaration: 

Syntax:

Method without Parameters:


return type is nothing but data type, so it can be either int , long, double etc...

use-case: (understanding how methods can be created)

1. My friend asked me what is your father name? I replied that my father name was Anjaneyulu. Here i am replying(returning) to my friend by saying my father name. So, return value is father name(String)

         String whatIsFatherName(){

              return "Anjaneyulu";
          
               }


2. Now, My friend asked me to keep switch on the light, i switched it on.
Here i am not replying anything to my friend, once i switched on light my task is done. So, here i am returning nothing which we used to call as void.

       void switchOnLight(){


                   
                            }

Method with Parameters:


         

Note: Suppose one shirt cost for laundering is 20 and one pant cost for laundering is 30, then total amount to be paid by suresh(row 1 in the top table) is:

2 shirts * 20 = 40
2 pants * 30 = 60
Total = 100;

So, we will pass number of shirts and number of shirts to totalAmount(int shirts, int pants) method as above.


Note: Class can also be called as collection of properties and functions.

Lets create class for a user who comes for laundering.


public class User {

    //properties

    //name of the user 
 String name_of_user;

    //number of shirts 
 int number_of_shirts_of_user;

    //number of pants 
 int number_of_pants_of_user;

    //total amount to be paid 
 int total_amount_to_be_paid;

    //advance paid 
 int advance_paid_by_user;

    //is laundry completed? 
 boolean is_laundry_completed;

    //is cloths delivered to the address 
 boolean is_delivered;

    //address of the user 
 String address_of_user;


    //methods/ functionalities
 
    //to get the name of the user, since it is returning data of type String , return type is String 
 
 public String getName() {
        return name_of_user;
    }

    //setting the name of the user, here, we are just setting the name user but we are not getting anything back from this method, so the return type is void.     
 
public void setName(String name) {
        name_of_user = name;
    } 
 
 public int getNumber_of_shirts() {
        return number_of_shirts_of_user;
    } 
 
 //Other below methods are declared for purpose as above
 
 
  public void setNumber_of_shirts(int number_of_shirts) {
        number_of_shirts_of_user = number_of_shirts;
    }

    public int getNumber_of_pants() {
        return number_of_pants_of_user;
    }

    public void setNumber_of_pants(int number_of_pants) {
        number_of_pants_of_user = number_of_pants;
    }

    public int getTotal_amount() {
        return total_amount_to_be_paid;
    }

    public void setTotal_amount(int total_amount) {
        total_amount_to_be_paid = total_amount;
    }

    public int getAdvance_paid() {
        return advance_paid_by_user;
    }

    public void setAdvance_paid(int advance_paid) {
        advance_paid_by_user = advance_paid;
    }

    public boolean isLaundry_completed() {
        return is_laundry_completed;
    }

    public void setLaundry_completed(boolean laundry_completed) {
        is_laundry_completed = laundry_completed;
    }

    public boolean isDelivered() {
        return is_delivered;
    }

    public void setDelivered(boolean delivered) {
        is_delivered = delivered;
    }

    public String getAddress() {
        return address_of_user;
    }

    public void setAddress(String address) {
        address_of_user = address;
    }
}
 
 




Now, We are familiar with Class, let's create object.

Object: (real world entity)

1. Objects are created from a class(template).
2. Syntax of creating object:

ClassName refVariable = new ClassName();

Creating object of User class.

//this is just like creating one row for suresh(as shown in the top image)

User suresh = new User();

Below image will depict what definitely will happen when we write above line.



As we can see fields will be initialized with default values.

To change those default values (just like updating suresh row with new values like No. of Shirts-2, No. of Pants-2, Total amount-100, Advance paid -50, laundry completed -no, address- palasa, delivered - no.

User the reference memory locatoin (suresh) and update with new values as below.

User suresh = new User();

suresh.name_of_user="Suresh";
suresh.number_of_shirts_of_user =2;
suresh.number_of_pants_of_user=2;
suresh.total_amount_to_be_paid=100;
suresh.advance_paid_by_user=50;
suresh.is_laundry_completed=false;
suresh.address="palasa";
suresh.is_delivered=false;

After two days you want to update suresh record as laudry is delivered, then we can call a method and update as below.

//calling method

suresh.setLaundry_completed(true);

Updating suresh record as delivered.

suresh.setDelivered(true);

Creating objects for Hareesh and Aravind.

User harresh = new User();
User aravind = new User();

Task for you: Now, update the hareesh and aravind records as shown the book above.

Inheritance: Acquiring properties and behavior of one object by another object.

Use: Reduces time in creating a new class

use-case: Designing a website will take less time if you already have a template, but it will take more time if you don't have a template.

Inheritance: You are using features of template and adding few more functionalities and requirement to that template.

Note: Similarly in java, we can create another class from an existing class.

For example i have a class named Bag:

class Bag{

String name;

int weight;

int color;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getWeight() {
    return weight;
}

public void setWeight(int weight) {
    this.weight = weight;
}

public int getColor() {
    return color;
}

public void setColor(int color) {
    this.color = color;
}

}


Creating WildCraftBag class from Bag class:

Here i am acquiring properties of Bag in WildCraftBag and adding someother properties.

class WildCraftBag extends Bag{

//default constructor

public WildCraftBag(){

}

//this is new features i am adding.

int number_of_pockets;
 
}

If we create object of WildCraftBag we can access(only in some cases) properties and functionalities of Bag as WildCraftBag is child class of Bag.

Creating object of WildCraftBag:

WildCraftBag wildcraft = new WildCraftBag();
//accessing properties of Bag class

wildcraft.name="WildCraft";
wildcraft.setWeight(1);//i mean 1kg weight

Note: We will discuss about Inheritance very detail in later chapters.

Note: We will discuss about other important concepts in detail later on...

Let's discuss few things in xml


No comments:

Post a Comment