Solved

Exemples on abstract class and interface

Posted on 1998-03-08
2
319 Views
Last Modified: 2008-03-10
I need some examples to undertand abstract class and interface.
0
Comment
Question by:Zeric
2 Comments
 
LVL 2

Accepted Solution

by:
shogi earned 200 total points
ID: 1233457
You can have 3 way to have an abstract class:
1 - not providing code of one or more method
2 - extends for abstract class and not providing code of one or more method of you extends class.
3 - just writing abstract before the class

About interface:
  Interface it's always abstract, but you don't need to write abstract, the keyword interface includ it.
  In interface you can only have abstract method and static final  constants.  And you need to implements it.

Look examples :
=============================================================
Example on 2 levels of abstract class

/*
     Project: abstract3
*/

import java.lang.*;
import java.io.*;
 
abstract class SuperObjet
{
    abstract public void Categorie();
}

abstract class Mammal extends SuperObjet
{
    public void IndirectCall()
    {
        Food();
    }

    abstract public void Food();
}


class Monkey extends Mammal
{
   public void Food()
   {
      System.out.println("Eat fruits");
   }
   public void Categorie()
   {
      System.out.println("Categorie = Singe");
   }
}

class Tiger extends Mammal
{
   public void Food()
   {
      System.out.println("Eat meats");
   }
   public void Categorie()
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
      System.out.println("Categorie = Tiger");
   }

}

public class abstract3
{
      static public void main(String args[])
      {
//        Vous n'avez pas le droit d'instancier une classe abstraite
//    Mammifere m = new Mammifere();

        Tiger  oTiger  = new Tiger();
        Monkey oMonkey = new Monkey();

        System.out.println("Direct call");
      oTiger.Food();
      oMonkey.Food();

      System.out.println("\nIndirect call to Food (polymorphism)");
      oTiger.IndirectCall();
      oMonkey.IndirectCall();

      System.out.println("\nCategories Methods");
      oTiger.Categorie();
      oMonkey.Categorie();
   }
}

=============================================================
=============================================================
Example on implementing 2 interfaces :

/*
    Projet: interface3
*/

interface TheMother
{
   static final int MotherConst = 100;
   abstract public void MotherMethod();
}

interface First extends TheMother
{
   static final int FirstConst = 200;

   abstract public void FirstMethod();
}

interface Second
{
   static final int SecondConst = 300;
   abstract public void SecondMethod();  
}

class interface3 implements First, Second
{
   public void FirstMethod()
   {
      System.out.println("Calling FirstMethod");
   }

   public void MotherMethod()
   {
      System.out.println("Calling MotherMethod");
   }
   
   public void SecondMethod()
   {
      System.out.println("Calling SecondMethod");
   }

   static public void main(String argv[])
   {
      interface3 interf = new interface3();

      interf.FirstMethod();
      System.out.println("the value of FirstConst is " + FirstConst);
      interf.MotherMethod();
      System.out.println("the value of MotherConst is " + MotherConst);
      interf.SecondMethod();
      System.out.println("the value of SecondConst is " + SecondConst);
   }
}
===============================================================
===============================================================

I hope that it will help you!
0
 

Author Comment

by:Zeric
ID: 1233458
Thanks Shogi
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now