OOPs Concepts for Android Programming – Part 2

OOPs Concepts for Android Programming

In the previous article i have discussed several OOPS concepts for Android Programming that everyone required to know for Android Application Development. If you did not have read it, please go through it once so you can easily understand topics from this article.


Inheritance is a concept where one object can acquires properties (data members) and behaviors (methods) of other object (the Object that current Object extends). It forms a relationship like Parent and Child. The class that extends other class will become Child and class to which current class extends become the Parent. Main advantage of inheritance is re-usability of data members and methods of parent class, so child class doesn’t have to modify or alter the implementation of parent class.

Inheritance used to implement the run time polymorphism (Method Overriding) that is a useful concept on programming that i have discussed later in this article.


class Subclass-name extends Superclass-name
    //methods and fields

Moreover, there are different types of Inheritance that we can implement in our system to achieve required specific functionality. Those types are as below,

  • Single Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

One of this all, Multiple Inheritance is not supported through extending multiple classes at one time but it can achieve through a concept called ‘an Interface’.

Example: Inheritance

class Employee{
    float salary=40000;
class Programmer extends Employee{
    int bonus=10000;
    public static void main(String args[]){
        Programmer p=new Programmer();
        System.out.println("Programmer salary is:"+p.salary);
        System.out.println("Bonus of Programmer is:"+p.bonus);



Apart from inheritance, a class can also acquire properties (data members) and behaviors (methods) of other class through Aggregation. According to it, a class can have an entity reference of other class that is known as an Aggregation. Aggregation makes code reusable to other parts of the whole program. To know about it see below example.

Example: Aggregation

class Employee{
    int id;
    String name;
    Address address; //Address is a class

Inheritance shows IS-A relationship between two objects/entities but when there is no IS-A relationship can be form, we can always make use of HAS-A relationship that can be achieve through Aggregation. From above example, it is clear that an Employee can’t be an Address but it can have an Address. So it derives a HAS-A relationship. (Employee has-a(an) address) .

Method Overriding

As i have discussed in Inheritance section, one class can acquire properties and data members of other class by extending them, method overriding work on that scenario. We can override method declared/implemented in Super class by declaring same method in the sub class with same name but with different implementation. We can use method overriding when we want different implementation of a method that already been defined in the parent classes. Method overriding also provides run time polymorphism.

There are some rules of Method Overriding as below:

  • Method must have the same name as the parent class
  • Method must have the same number of parameter as the parent class (Keep this point clear it is different from Method overloading where we can change the either number or data type of passing parameters of method.)
  • There must be a IS-A relationship between method that we implement with change.

Example: Method Overriding

class Bank{
    int getRateOfInterest(){return 0;}
class SBI extends Bank{
    int getRateOfInterest(){return 8;}
class ICICI extends Bank{
    int getRateOfInterest(){return 7;}


As a programmer we can put some portion of the code segment into a single unit, so it can be reside in its own boundary away from main code segment. Mechanism to wrap code and data together into a single unit is the Encapsulation. One of the example of this is a Class because it contain all data member and properties bind to itself but it is not fully encapsulated one. On the other hand, fully encapsulated class contain all data members private and it uses getter & setter for the purpose of get & set data values respectively.

Encapsulation gives advantages to make class read only or write only and also provides control over the data that we declare inside class region.

Example: Encapsulation

//save as Student.java
public class Student{
    private String name;

    public String getName(){
        return name;
    public void setName(String name){

//save as Test.java
class Test{
    public static void main(String[] args){
        Student s=new Student();

So these are all other several Object Oriented Programming Concepts for Android Programming that i have discussed here. Few other concepts are remaining which i’m about to discussed in my next article of this Android Programming articles.

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

Leave a Reply

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