oops concepts - android programming

29
OOPs CONCEPTS FOR ANDROID PROGRAMMING OBJECT ORIENTED PROGRAMMING system Purvik Rana

Upload: purvik-rana

Post on 12-Apr-2017

419 views

Category:

Education


4 download

TRANSCRIPT

Page 1: OOPs Concepts - Android Programming

OOPs CONCEPTS FOR ANDROID PROGRAMMING

OBJECT ORIENTED PROGRAMMING system

Purvik Rana

Page 2: OOPs Concepts - Android Programming

intro A programming paradigm where everything is represented as

• an object.

A methodology to design a program using • Classes and Objects .

Simplifies the software development process & maintenance by rich concepts like

• Object • Polymorphism

• Class • Abstraction

• Inheritance • Encapsulation

Page 3: OOPs Concepts - Android Programming

Intro - continue

Object

• Entity that has State (Property) and Behaviours (Methods).

• For Example: Person, Car, House, Chair.

Class

• Collection of Objects ( a logical entity ).

Inheritance

• When one object acquires all the properties & behaviours of parent object provides code Reusability.

Polymorphism

• Where one task perform in different ways (overloading & overriding).

Page 4: OOPs Concepts - Android Programming

Intro - continue

Abstraction

• Hiding internal details and display only functionality.

• It is achieved through Abstract class & Interface.

Encapsulation

• Binding Code and Data together into a single unit.

• A Java Class is an example of it where all the data members and methods are private to it.

Page 5: OOPs Concepts - Android Programming

Naming Conventions Name Convention

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

Page 6: OOPs Concepts - Android Programming

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);

}

}

Page 7: OOPs Concepts - Android Programming

Method Overloading What

• Class has method with same name but with different parameters.

• Increase readability of the program.

Two ways to perform:

• Either change the number of arguments.

• Change the data type of arguments.

• Not possible by changing the return type of method.

Page 8: OOPs Concepts - Android Programming

Method overloading - Example Example-1 Different No of Parameters

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

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);

} }

Page 9: OOPs Concepts - Android Programming

Constructor What

• Special type of method used to initialize the object.

• Invoked at the time of object creation.

Rules

• Name must be same as its class name.

• Must have no explicit return type.

Type

• Default Constructor.

• Parameterized Constructor.

Constructor Overloading

• Same as method overloading

Page 10: OOPs Concepts - Android Programming

Constructor - example class Student5{

int id;

String name;

int age;

Student5() { } // default

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();

}

}

Page 11: OOPs Concepts - Android Programming

Inheritance What

• One object acquires all the properties & behaviours of parent object. • Reuse parameters and method of parent class. • Holds IS-A relationship.

Why • Method Overriding (For run time polymorphism) • Code reusability

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

Page 12: OOPs Concepts - Android Programming

Inheritance - continue

Types • Multiple inheritance not supported

in Java through Classes

• Supported through “Interfaces”

Page 13: OOPs Concepts - Android Programming

Inheritance - example 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);

}

}

Page 14: OOPs Concepts - Android Programming

Aggregation When class has an entity reference called Aggregation.

Why for code reusability.

class Employee{

int id;

String name;

Address address; //Address is nothing but some other class

...

}

Relationship is : Employee HAS-A address.

Used when there is no IS-A relationship.

Page 15: OOPs Concepts - Android Programming

Method Overriding When subclass override same method as declared in Super Class.

Usage

• Subclass provides specific implementation of method that it overrides.

• Used for runtime polymorphism.

Rules

• Method must have same name as the parent class

• Must have same parameter as the parent class

• Must be a IS-A relationship

Page 16: OOPs Concepts - Android Programming

Method overriding - Example Example

class Bank{

int getRateOfInterest(){return 0;}

}

class SBI extends Bank{

int getRateOfInterest(){return 8;}

}

class ICICI extends Bank{

int getRateOfInterest(){return 7;}

}

Page 17: OOPs Concepts - Android Programming

Abstract Class What

• Class declared with Abstract keyword.

• Can have abstract & non-abstract methods.

Abstraction • Used to hide implementation details from the user.

• Focus on “what it does” not reveal “how it does”.

Two ways to achieve, thorough • Abstract Class

• Interface

Page 18: OOPs Concepts - Android Programming

Abstract class - CONTINUE Example

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(); } }

Page 19: OOPs Concepts - Android Programming

Interface What

• Is a blueprint of a class and abstract methods only.

• Should have only abstract methods in Java interface.

• Parameters are public, static & final.

• Methods are public & abstract.

Why

• To achieve full abstraction.

• To support multiple inheritance.

• To achieve loose coupling.

Page 20: OOPs Concepts - Android Programming

Interface- example -1

Understand Relationship Example:

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();

} }

Page 21: OOPs Concepts - Android Programming

Interface- example -2

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();

} }

Page 22: OOPs Concepts - Android Programming

encapsulation Wrapping code & data together. into a single unit

Fully encapsulated class contain

• All data member as private.

• Use setter & getter method to set and get the data values.

Advantages

• Make class read only or write only

• Provides control over the data

Page 23: OOPs Concepts - Android Programming

Encapsulation - example

//save as Student.java

public class Student{

private String name;

public String getName(){

return name;

}

public void setName(String name){

this.name=name

}

}

//save as Test.java

class Test{

public static void main(String[] args){

Student s=new Student();

s.setname("vijay");

System.out.println(s.getName());

}

}

Page 24: OOPs Concepts - Android Programming

super keyword Is a reference variable used to refer immediate parent class object

Usage

• To refer immediate parent class instance variable.

• To invoke immediate parent class constructor.

• To invoke immediate parent class method.

Page 25: OOPs Concepts - Android Programming

Super keyword - example

Example - 1

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();

} }

Example - 2

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();

} }

Example – 3

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();

} }

Page 26: OOPs Concepts - Android Programming

This keyword Is a reference variable used to refer current objects

Usage

• to refer current class instance variable

• to invoke current class constructor

• can be passed as an argument in the method call

• can be passed as argument in the constructor call

• can also be used to return the current class instance

Page 27: OOPs Concepts - Android Programming

This keyword - example Example - 1

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();

} }

Example – 2

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();

} }

Page 28: OOPs Concepts - Android Programming

THIS IS WHAT ALL WE’VE LEARN

• OBJECT & CLASS

• INHERITANCE

• ABSTRACTION

• ENCAPSULATION

• POLYMORPHISM

• Super & This Keyword

Page 29: OOPs Concepts - Android Programming

SE CONCEPTS FOR ANDROID PROGRAMMING

Next Session