OOPs Concepts for Android Programming – Part 1

OOPs Concepts for Android Programming

There might be a confusion about what importance, OOPs concepts will take in Android programming. But as core part of Android Programming, JAVA gives better programming paradigm for developer to take advantage of all Object Oriented Concepts. Here in this article, i’m about to discuss OOPs concepts for Android Programming that every one should know about before heading for the Android Application Programming.

Intro

Object Oriented Programming is nothing but it’s a programming paradigm where everything is represented as an Object. It is a methodology to design a program using Class and Objects. OOPs concepts simplifies the software development process as well as maintenance by providing some rich concepts like,

  • Object
    • It act as an Entity that has State (Property)  and Behaviors (Methods).
    • For example: Person, Car, House, Chair
  • Class
    • It is a collection of Objects
  • Inheritance
    • Inheritance exist when one object acquires all the properties and behaviors of parent object for behavior reuse or modification.
    • It provides code reusability and better programming paradigm.
  • Polymorphism
    • Where one task perform in different ways & that can be implemented using Method Overriding and Method Overloading.
  • Abstraction
    • It gives access to hide internal details and to display only functionality to the user.
    • It is achieved through Abstract class and Interface.
  • Encapsulation
    • It helps you to bind code as well as data together into a single unit.
    • A java class is an example of it where data members and methods are private to it.

Naming Conventions

  • Class Name
    • Should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
  • Interface Name
    • Should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
  • Method Name
    • Should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
  • Variable Name
    • Should start with lowercase letter e.g. firstName, orderNumber etc.
  • Package Name
    • Should be in lowercase letter e.g. java, lang, sql, util etc.
  • Constants Name
    • Should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.

CamelCase : If name is combined with two words, second word will start with uppercase letter always

Class Declaration

Example : Class Declaration

class Student1{
    int id; //data member (also instance variable)
    String name; //data member(also instance variable)
  
  public static void main(String args[]){
    Student1 s1=new Student1(); //creating an object of Student
    System.out.println(s1.id); // accessing class data member
    System.out.println(s1.name);
  }
}

Method Overloading

Method overloading helps to achieve Polymorphism in the programming. So you can defined one task and make use of that class with different implementation as required by your side. You can define a method with same name but with different parameters to achieve it. What it helps you, it increase the readability of the program.

There is two ways to perform this:

  • Either change number of argument of method that has the same name
  • or change data type of arguments of method that has same name

Note: It is not possible by changing the return type of the method that has the same name

Example -1 : Different No of Arguments

class Calculation{
    void sum(int a,int b){
        System.out.println(a+b);    
    }
    void sum(int a,int b,int c){
        System.out.println(a+b+c);
    }
    public static void main(String args[]){
    Calculation obj=new Calculation();
    obj.sum(10,10,10);
    obj.sum(20,20);
    }
}

Example -2 : Changing Data Type of Arguments

class Calculation2{
    void sum(int a,int b){
        System.out.println(a+b);    }

    void sum(double a,double b){
        System.out.println(a+b);    }

public static void main(String args[]){

    Calculation2 obj=new Calculation2();
    obj.sum(10.5,10.5);
    obj.sum(20,20);
    }
}

Constructor

Constructor is a special type of method to initialize the object. Constructors are invoked at the time of object creation.

There are some rules to define constructor that are as below.

  • It’s name must be same as it’s class name.
  • It must have no explicit return type.

Different types of Constructors are,

  • Default Constructor
    • Where constructor has definition as the its class. It does not have any arguments to handle and that’s why it is default constructor.
  • Parameterized Constructor
    • Where constructor has definition as the its class but it have few arguments to handle and that’s why it is Parameterized constructor.

Constructor can also be overloaded by taking different arguments with different constructor of the same name. The implementation will remain same as we done in method overloading.

Example  : Constructor

class Student5{
    int id;
    String name;
    int age;
    
    Student() { }
    
    Student5(int i,String n){
        id = i;
        name = n;
    }

    Student5(int i,String n,int a){
        id = i;
        name = n;
        age=a;
    }

void display(){
    System.out.println(id+" "+name+" "+age);    }

public static void main(String args[]){
    Student5 s1 = new Student5(111,"Karan");
    Student5 s2 = new Student5(222,"Aryan",25);
    s1.display();
    s2.display();
    }
}

The other OOPs concepts  are also there, that i will discuss in my next article of this main article series.

 

“Bless for you, peace for the World” – Purvik

Leave a Reply

Your email address will not be published. Required fields are marked *