Android Inheritance


  1. Single Inheritance in Android. 
  2. Multi-level Inheritance in Android. 
  3. Multiple Inheritance in Android. 
  

1.Single Inheritance in Android. 

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="38dp"   
     android:text="Single Inheritance" />   
   <TextView   
     android:id="@+id/textView1"   
     android:textSize="20dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_alignParentTop="true"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="125dp" />   
 </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 single;   
 TextView tv;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     single = (Button) findViewById(R.id.button1);   
     tv = (TextView) findViewById(R.id.textView1);   
     single.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 B obj = new B();   
 tv.setText(obj.display());   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
 class A   
 {   
 protected String str_A = "Hello ";   
 }   
 class B extends A   
 {   
 String str_B = str_A.concat("Hemanth Somaraju !!!");   
 String display()   
 {   
 return str_B;   
 }   
 }   


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.Multi-level Inheritance in Android. 

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="50dp"   
     android:text="Multi-Level Inheritance" />   
   <TextView   
     android:id="@+id/textView1"   
     android:textSize="25dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="170dp" />   
 </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 Multilevel;   
 TextView tv;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     Multilevel = (Button) findViewById(R.id.button1);   
     tv = (TextView) findViewById(R.id.textView1);   
     Multilevel.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 C obj = new C();   
 tv.setText(obj.display());   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
 class A   
 {   
 protected String str_A = "By ";   
 }   
 class B extends A   
 {   
 protected String str_B = str_A.concat("Hemanth ");   
 }   
 class C extends B   
 {   
 String str_C = str_B.concat("Somaraju");   
 String display()   
 {   
 return str_C;   
 }   
 }   


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 
  
3.Multiple Inheritance in Android. 
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="78dp"   
     android:text="Multiple Inheritance" />   
   <TextView   
     android:id="@+id/textView1"   
     android:textSize="20dp"   
     android:gravity="center"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_centerHorizontal="true"   
     android:layout_centerVertical="true" />   
 </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 multiple;   
 TextView tv;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     multiple = (Button) findViewById(R.id.button1);   
     tv = (TextView) findViewById(R.id.textView1);   
     multiple.setOnClickListener(new View.OnClickListener() {   
 public void onClick(View v) {   
 // TODO Auto-generated method stub   
 H obj = new H();   
 tv.setText(obj.display());   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   
 interface A   
 {   
 String str_A = "H";   
 }   
 interface B   
 {   
 String str_B = "E";   
 }   
 interface C   
 {   
 String str_C = "M";   
 }   
 interface D   
 {   
 String str_D = "A";   
 }   
 interface E   
 {   
 String str_E = "N";   
 }   
 interface F   
 {   
 String str_F = "T";   
 }   
 interface G   
 {   
 String str_G = "H";   
 }   
 class H implements A, B, C, D, E, F, G   
 {   
 String str_H = str_A.concat(str_B).concat(str_C).concat(str_D).   
 concat(str_E).concat(str_F).concat(str_G);   
 String display()   
 {   
 return str_H;   
 }   
 }   










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 :
  
Image Image