Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //Extends
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button bt = (Button) findViewById(R.id.button1);
- //Anonymous Class / Inner Class
- bt.setOnClickListener(new View.OnClickListener() {
- //Callback Function / Override
- public void onClick(View v) {
- //Method Chaining
- Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- Extends
- public class Init {
- public static void main(String[] args) {
- new Son();
- }
- }
- class Son extends Father {
- Son(){
- eat();
- }
- }
- class Father {
- public void eat(){
- System.out.println("Orange");
- }
- }
- Override
- public class Init {
- public static void main(String[] args) {
- new Son();
- }
- }
- class Son implements Father {
- Son(){
- }
- @Override
- public void eat(){
- System.out.println("Apple");
- }
- }
- interface Father {
- public void eat();
- }
- Callback Function
- public class Init {
- public static void main(String[] args) {
- Son cup = new Son();
- cup.drink(new Father(){ //Anonymous Class
- void eat(){ //Callback Function
- System.out.println("Apple");
- }
- });
- }
- }
- class Son {
- void drink(Father cup){
- cup.eat();
- }
- }
- class Father {
- void eat(){
- }
- }
- Anonymous Class
- public class Init {
- public static void main(String[] args) {
- new Son(); //Anonymous class
- }
- }
- class Son {
- Son(){
- //drink("Coffee", new Father());
- drink("Coffee", new Father(){ //Anonymous class
- void eat(){
- System.out.println("Orange");
- }
- });
- }
- void drink(String cup1, Father cup2){
- System.out.println(cup2.Father(cup1));
- cup2.eat();
- }
- }
- class Father {
- String Father(String cup){
- return cup;
- }
- void eat(){
- System.out.println("Banana");
- }
- }
- Inner Class
- public class Init {
- public static void main(String[] args) {
- new Son();
- new Son.Sson().eat(); // inner class
- }
- }
- class Son {
- Son(){
- System.out.println("Banana");
- }
- static class Sson{
- void eat(){
- System.out.println("Apple");
- }
- }
- }
- Method Chaining
- public class Init {
- public static void main(String[] args) {
- new Son().drink().eat().desert(); //Method Chaining
- }
- }
- class Son {
- Son(){
- }
- Son drink(){
- System.out.println("Milk");
- return this;
- }
- Son eat(){
- System.out.println("Beef");
- return this;
- }
- void desert(){
- System.out.println("Cake");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment