Solved

Exemples on abstract class and interface

Posted on 1998-03-08
2
322 Views
Last Modified: 2008-03-10
I need some examples to undertand abstract class and interface.
0
Comment
Question by:Zeric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

749 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