[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Exemples on abstract class and interface

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

650 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