OOPs Concepts for Android Programming – Part 3

OOPs Concepts for Android Programming

In the previous two articles i have discussed several OOPs Concepts for Android Programming that must be known to us prior to go for Android Application Development. If you did not have read those, please go through them once so you can easily understand topics from this article.

Abstrat Class

One of the form a class can take is an Abstract’ form. To derive a class as an abstract class, declare a class with Abstract keyword. We can declare abstract and non-abstract methods inside an abstract class. Abstraction is mainly used to hide implementation details from the user. It focus on ‘What it does’, but not reveal a single information about ‘how it actually does’.

There is two ways by which we can achieve abstraction,

  • Using an Abstract Class
  • Using an Interface

Example: Abstract Class

abstract class Bike{
    abstract void run();
}
class Honda4 extends Bike{
    void run(){System.out.println("running safely..");}
public static void main(String args[]){
    Bike obj = new Honda4();
    obj.run();
    }
}

Interface

Interface is nothing but it is a blueprint of a class and abstract methods. We can implement only abstract methods in an interface. Declared parameters should be public, static and final where, methods can be public and abstract in an interface. Interface implementation achieves full abstraction, support of multiple inheritance and also gives loose coupling.

Understand Relationship:

Single Inheritance, OOPs Concepts for Android Programming

Single Inheritance

Example : Single inheritance

interface printable{
    void print();
}
class A6 implements printable
{
    public void print(){
        System.out.println("Hello");}
    
    public static void main(String args[]){
            A6 obj = new A6();
            obj.print();
        }
}
Multiple Inheritance, OOPs Concepts for Android Programming

Multiple Inheritance

 

Example : Multiple inheritance

interface Printable{
    void print(); }
interface Showable{
    void show(); }
class A7 implements Printable,Showable{
    public void print(){
        System.out.println("Hello");}

    public void show(){
        System.out.println("Welcome");}

    public static void main(String args[]){
        A7 obj = new A7();
        obj.print();
        obj.show();
    }
}

 

Super keyword

Super keyword is a reference variable used to refer immediate parent class object. There are three usage of super keyword in programming.

  • To refer immediate parent class instance variable.

Example: 

class Vehicle{
    int speed=50;
}
class Bike4 extends Vehicle{
    int speed=100;
    void display(){
        System.out.println(super.speed);
        //will print speed of Vehicle now
    }

    public static void main(String args[]){
        Bike4 b=new Bike4();
        b.display();
    } 
}

  • To invoke immediate parent class constructor.

Example: 

class Vehicle{
    Vehicle(){
        System.out.println("Vehicle is created");
    }
}
class Bike5 extends Vehicle{
    Bike5(){
        super();
        //will invoke parent class constructor
        System.out.println("Bike is created");
    }

    public static void main(String args[]){
        Bike5 b=new Bike5();
    }
}

  • To invoke immediate parent class method.

Example: 

class Person{
    void message(){
        System.out.println("welcome");
    }
}
class Student16 extends Person{
    void message(){
        System.out.println("welcome to java");
    }
    void display(){
        message();
        //will invoke current class message() method
        super.message();
        //will invoke parent class message() method
    }
    public static void main(String args[]){
        Student16 s=new Student16();
        s.display();
    }
}

This keyword

This keyword is a reference variable used to refer current objects. There are several usage of this keyword in the programming that are as below.

  • Used to refer current class instance variable.

Example: 

class Student11{
    int id;
    String name;
    Student11(int id,String name){
        this.id = id;
        this.name = name;
    }
    void display(){
        System.out.println(id+" "+name);
    }
    public static void main(String args[]){
        Student11 s1 = new Student11(111,"Karan");
        Student11 s2 = new Student11(222,"Aryan");
        s1.display();
        s2.display();
    }
}

  • Used to invoke current class constructor.

Example: 

class Student13{
    int id;
    String name;
    Student13(){
        System.out.println("default constructor is invoked");
    }
    Student13(int id,String name){
        this ();//it is used to invoked current class constructor.
        this.id = id;
        this.name = name;
    }
    void display(){
        System.out.println(id+" "+name);
    }
    public static void main(String args[]){
        Student13 e1 = new Student13(111,"karan");
        Student13 e2 = new Student13(222,"Aryan");
        e1.display();
        e2.display();
    } 
}

Apart from these, several other use of this keyword are,

  • Can be passed as an argument in a Method call.
  • Can be passed as argument in a Constructor call.
  • Can also be used to return a current class instance.

 

So all these Object Oriented Programming Concepts for Android Programming have equal importance as your own ideas of Application Development. Make sure you practice them frequent or study them well before heading towards the Android App Development. We all know these all OOPS concepts gonna help us out very much in Android Programming & this is what you should know all about Object Oriented Programming Concepts for Android Programming. Go through all three articles of OOPS concept that i have published prior to this one to know from the depth.

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

Leave a Reply

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