Showing posts with label Android Polymorphism. Show all posts
Showing posts with label Android Polymorphism. Show all posts

Android Polymorphism


  1. Same Length Attributes Method Overloading. 
  2. Different Length Attributes Method Overloading. 
 

1.Same Length Attributes Method Overloading. 

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
  
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code : 

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   xmlns:tools="http://schemas.android.com/tools"   
   android:layout_width="match_parent"   
   android:layout_height="match_parent" >   
   <TextView   
     android:id="@+id/textView1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="60dp"   
     android:text="Polymorphism - Same Length       By Hemanth Somaraju"   
     android:textSize="20dp" />   
   <Button   
     android:id="@+id/button1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/textView1"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="40dp"   
     android:text="Get" />   
   <TextView   
     android:id="@+id/textView2"   
     android:textSize="18dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="220dp"   
     android:text="1" />   
   <TextView   
     android:id="@+id/textView3"   
     android:textSize="18dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="270dp"   
     android:text="2" />   
   <TextView   
     android:id="@+id/textView4"   
     android:textSize="18dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="320dp"   
     android:text="3" />   
   <TextView   
     android:id="@+id/textView5"   
     android:textSize="18dp"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="370dp"   
     android:text="4" />   
 </RelativeLayout>  


Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.view.View;   
 import android.widget.Button;   
 import android.widget.TextView;   
 public class MainActivity extends Activity {   
 Button b;   
 TextView tv1, tv2, tv3, tv4;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     b = (Button) findViewById(R.id.button1);   
     tv1 = (TextView) findViewById(R.id.textView2);   
     tv2 = (TextView) findViewById(R.id.textView3);   
     tv3 = (TextView) findViewById(R.id.textView4);   
     tv4 = (TextView) findViewById(R.id.textView5);   
     b.setOnClickListener(new View.OnClickListener() {   
 @Override   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 SubClass obj = new SubClass();   
 String str1 = obj.method_overloading(10, 10);   
 String str2 = obj.method_overloading(10, (float)10.123);   
 String str3 = obj.method_overloading((float)10.123, 10);   
 String str4 = obj.method_overloading((float)10.123, (float)10.123);   
 tv1.setText(str1);   
 tv2.setText(str2);   
 tv3.setText(str3);   
 tv4.setText(str4);   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
 class SubClass {   
 String method_overloading(int a, int b)   
 {   
 return "(int, int) Method Called";   
 }   
 String method_overloading(int a, float b)   
 {   
 return "(int, float) Method Called";   
 }   
 String method_overloading(float a, int b)   
 {   
 return "(float, int) Method Called";   
 }   
 String method_overloading(float a, float b)   
 {   
 return "(float, float) Method Called";   
 }   
 }   


Step 4 : Open AndroidManifest.xml and add following code :

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   


Step 5 : Our output will be like this : 
  
ImageImage 
    
2.Different Length Attributes Method Overloading. 
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 
  
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :


 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
   xmlns:tools="http://schemas.android.com/tools"   
   android:layout_width="match_parent"   
   android:layout_height="match_parent" >   
   <Button   
     android:id="@+id/button1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="20dp"   
     android:text="Polymorphism" />   
   <TextView   
     android:id="@+id/textView1"   
     android:textSize="15dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="100dp" />   
   <TextView   
     android:id="@+id/textView2"   
     android:textSize="15dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="150dp" />   
   <TextView   
     android:id="@+id/textView3"   
     android:textSize="15dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="200dp" />   
   <TextView   
     android:id="@+id/textView4"   
     android:textSize="15dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="250dp" />   
   <TextView   
     android:id="@+id/textView5"   
     android:textSize="15dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="300dp" />   
   <TextView   
     android:id="@+id/textView6"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/button1"   
     android:layout_centerHorizontal="true"   
     android:text="Hemanth Somaraju"   
     android:textAppearance="?android:attr/textAppearanceLarge" />   
 </RelativeLayout>  

Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.view.View;   
 import android.widget.Button;   
 import android.widget.TextView;   
 public class MainActivity extends Activity {   
 Button poly;   
 TextView t1, t2, t3, t4, t5;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     poly = (Button) findViewById(R.id.button1);   
     t1 = (TextView) findViewById(R.id.textView1);   
     t2 = (TextView) findViewById(R.id.textView2);   
     t3 = (TextView) findViewById(R.id.textView3);   
     t4 = (TextView) findViewById(R.id.textView4);   
     t5 = (TextView) findViewById(R.id.textView5);   
     poly.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 SubClass obj = new SubClass ();   
 t1.setText(String.valueOf(obj.display()));   
 t2.setText(String.valueOf(obj.display(11)));   
 t3.setText(String.valueOf(obj.display((float)15.26)));   
 t4.setText(String.valueOf(obj.display(11, 20)));   
 t5.setText(obj.display("Overloading"));   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
 class SubClass   
 {   
 int display()   
 {   
 int a = 23;   
 return a;   
 }   
 int display(int temp1)   
 {   
 int a = 23;   
 a = a + temp1;   
 return a;   
 }   
 int display(int temp1, int temp2)   
 {   
 int c;   
 c = temp1 + temp2;   
 return c;   
 }   
 float display(float temp1)   
 {   
 float c;   
 c = temp1 * 123 ;   
 return c;   
 }   
 String display(String temp1)   
 {   
 String str = "Method ";   
 str = str.concat(temp1);   
 return str;   
 }   
 } 


Step 4 : Open AndroidManifest.xml and add following code : 
 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="18" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.MainActivity"   
       android:label="@string/app_name" >   
       <intent-filter>   
         <action android:name="android.intent.action.MAIN" />   
         <category android:name="android.intent.category.LAUNCHER" />   
       </intent-filter>   
     </activity>   
   </application>   
 </manifest>   
Step 5 : Our output will be like this : 

ImageImage