?
Solved

Exemples on abstract class and interface

Posted on 1998-03-08
2
Medium Priority
?
326 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 800 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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