Solved

Exemples on abstract class and interface

Posted on 1998-03-08
2
320 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 68
Which is the latest version of Apache Tomcatl 3 50
Responding to Java JComponent extended classes's resize event 4 50
java stored proc example 9 20
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

15 Experts available now in Live!

Get 1:1 Help Now