OOPs CONCEPTS FORANDROIDPROGRAMMING
OBJECT ORIENTED PROGRAMMING system
Purvik Rana
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
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).
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.
NamingConventions
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
ClassDeclaration
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);
}
}
MethodOverloading
 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.
Methodoverloading - 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);
} }
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
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();
}
}
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
}
Inheritance - continue
 Types
• Multiple inheritance not supported
in Java through Classes
• Supported through “Interfaces”
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);
}
}
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.
MethodOverriding
 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
Methodoverriding- Example
Example
class Bank{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
AbstractClass
 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
Abstractclass - 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();
}
}
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.
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();
}
}
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();
} }
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
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());
}
}
superkeyword
 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.
Superkeyword- 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();
} }
Thiskeyword
 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
Thiskeyword - 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("defaultconstructor 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();
} }
THIS IS WHATALLWE’VE LEARN
• OBJECT & CLASS
• INHERITANCE
• ABSTRACTION
• ENCAPSULATION
• POLYMORPHISM
• Super & This Keyword
SE CONCEPTS FORANDROIDPROGRAMMING
Next Session

OOPs Concepts - Android Programming

  • 1.
    OOPs CONCEPTS FORANDROIDPROGRAMMING OBJECTORIENTED PROGRAMMING system Purvik Rana
  • 2.
    intro  A programmingparadigm 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
  • 3.
    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).
  • 4.
    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.
  • 5.
    NamingConventions Name Convention class name shouldstart 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
  • 6.
    ClassDeclaration 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); } }
  • 7.
    MethodOverloading  What • Classhas 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.
  • 8.
    Methodoverloading - Example Example-1Different 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); } }
  • 9.
    Constructor  What • Specialtype 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
  • 10.
    Constructor - example classStudent5{ 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(); } }
  • 11.
    Inheritance  What • Oneobject 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 }
  • 12.
    Inheritance - continue Types • Multiple inheritance not supported in Java through Classes • Supported through “Interfaces”
  • 13.
    Inheritance- example class Employee{ floatsalary=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); } }
  • 14.
    Aggregation  When classhas 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.
  • 15.
    MethodOverriding  When subclassoverride 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
  • 16.
    Methodoverriding- Example Example class Bank{ intgetRateOfInterest(){return 0;} } class SBI extends Bank{ int getRateOfInterest(){return 8;} } class ICICI extends Bank{ int getRateOfInterest(){return 7;} }
  • 17.
    AbstractClass  What • Classdeclared 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
  • 18.
    Abstractclass - CONTINUE Example abstractclass 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(); } }
  • 19.
    Interface  What • Isa 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.
  • 20.
    Interface-example-1 Understand Relationship Example: interface printable{ voidprint(); } class A6 implements printable{ public void print() {System.out.println("Hello");} public static void main(String args[]){ A6 obj = new A6(); obj.print(); } }
  • 21.
    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(); } }
  • 22.
    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
  • 23.
    Encapsulation- example //save asStudent.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()); } }
  • 24.
    superkeyword  Is areference 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.
  • 25.
    Superkeyword- 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(); } }
  • 26.
    Thiskeyword  Is areference 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
  • 27.
    Thiskeyword - 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("defaultconstructor 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(); } }
  • 28.
    THIS IS WHATALLWE’VELEARN • OBJECT & CLASS • INHERITANCE • ABSTRACTION • ENCAPSULATION • POLYMORPHISM • Super & This Keyword
  • 29.