Abstract methods in ByteBuffer class

I'am a little confused with abstract methods get() and put() in ByteBuffer class.
Java API says:
public abstract class ByteBuffer
extends Buffer implements Comparable
// This is a partial API listing
public abstract byte get( );
public abstract byte get (int index);
public abstract ByteBuffer put (byte b);
public abstract ByteBuffer put (int index, byte b);
}Question is:
How can I call methods get() and/or put() when they are abstract - not implemented in ByteBuffer class?
Thanks

Yeah, it's a subclass of ByteBuffer. You can find out more about the class by using introspection.
But, you don't have to, to use this API. This is called "design-by-contract" (well, part of it anyway), and that's why the class is hidden with respect to the API docs. The idea is that an API is kind of like a contract, an agreement between the designer and the user of the API. The user of the API is given just what s/he needs to do the job. The designer might implement the API with more stuff that is immediately apparent, but the user doesn't need to know that stuff to use the API. This provides a good layer of abstraction around the API and makes code more flexible.
So, it's good to know how these things work -- that the ByteBuffer contract is serviced by hidden implementing classes -- but you don't have to know that to use ByteBuffer, and in fact you'd probably only make things difficult (buggy and hard to maintain) if you wrote code that used the fact that apparently the implementing subclass of ByteBuffer is HeapByteBuffer.

Similar Messages

  • Abstract Method in a Class with implemented method

    I have a class which already has methods Implemented.I mean thse metgods are not abstract.I want only one method to be abstarct which will be overrideen by its subclass or derived Class
    I have never tried this
    Does anyone knows how to do this
    If yes could you give me syntax of the method
    Thanks in advance
    CSJakharia

    javax.swing.JComponent is an example of an abstract
    class with no abstract methods. That is why the
    following works:
    JComponent component = new JComponent(){};
    Not to forget you cannot instantiate abstarct classes
    public abstract class Test
    public String getName()
    return "Mike";
    public static void main(String[] args)
    Test tt = new Test();
    System.out.println(tt.getName());
    }and you would get the error
    The type Test cannot be instantiated.
    You remove the abstract keyword and it would compile
    good.No I am not misinterpreting I know what he is saying but I am closing the door of misinterpretation which I felt was possible. ;)
    cheers

  • About abstract method read() in class InputStream (third message)

    The subclass FilterInputStream is not abstract and extend InputStream: have you ever seen the implementation of that method in the source code available in SDK? It only calls that abstract method!!! I think it is not a very simple problem and anyway the answer is not in my diffrent books. Thanks for your non trivial answer.

    Please post this as a "reply" in your original thread
    http://forum.java.sun.com/thread.jspa?threadID=5192979&tstart=0
    Don't start a new thread, simply reply to one of the other posts in the original thread (there is a button "reply" on the right hand side).

  • Implement abstract method from base class problem

    Here is the example:
    public abstract class AbstractClass{
    protected double aVariable;
    protected abstract double abstractMethod();
    public class RealClass extends AbstractClass{
    public double abstractMethod(){
    return aVariable;
    Error message:
    RealClass should be declared abstract; it does not define abstractMethod() in AbstractClass

    I also compiled the code with Jdk1.3 and it
    worked.. The code would not have compiled
    if the access modifier in the derived class
    was more restrictive(eg private )...

  • About abstract method read() in class InputStream

    I would like to know if behind the method
    public abstract int read() throws IOException
    in the abstract class InputStream there is some code that
    is called when I have to read a stream. In this case where can I find
    something about this code and, if is written in other languages, why
    is not present the key word native?
    Thanks for yours answers and sorry for my bad english.

    Ciao Matteo.
    Scusa se ti rispondo in ritardo... ma ero in pausa pranzo.
    Chiedimi pure qualcosa di pi? specifico e se posso darti una mano ti rispondo.
    Le classi astratte sono utilizzate per fornire un comportamento standard lasciando per? uno o pi? metodi non implementati... liberi per le necessit? implementative degli utilizzatori.
    Nel caso specifico la classe InputStream ? una classe astratta che lascia non implementato il metodo read(). Tu nel tuo codice non utilizzerai mai questa classe come oggetto, ma nel caso specifico una sua sottoclasse che ha implementato il metodo read().
    Se vai nelle api di InputStream vedrai che ci sono diverse sottoclassi che estendono InputStream. Guarda ad esempio il codice di ByteArrayInputStream: in questa classe il metodo read() non ? nativo ma restituisce un byte appartenente al suo array interno.
    I metodi nativi (ad esempio il metodo read() della classe FileInputStream) non hanno implementazione java ma fanno invece riferimento a delle chiamate dirette al sistema operativo.
    Per quanto riguarda la classe FilterInputStream di cui parlavi: essa nel suo costruttore riceve un InputStream. Questo significa che si deve passare nel costruttore non la classe InputStream (che ? astratta) ma una classe che la estende e che quindi non sia astratta. Il motivo per il quale FilterInputStream faccia riferimento a una classe di tipo InputStream al suo interno, ? che in java gli stream di input e di output possono essere composti l'uno sopra l'altro per formare una "catena" (a tal proposito vedi per maggiori dettagli uno dei tani articoli che si trovano in rete.... ad esempio ti indico questo http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/). Comunque per dirla in due parole: tu puoi voler usare un FileInputStream per leggere un file, ma se hai bisogno di effettuare una lettura pi? efficiente (quindi bufferizzata) puoi aggiungere in catena al FileInputStream un oggetto di tipo FilterInputStream (nel caso specifico un BufferedInputStream che non ? altro che una sottoclasse di FilterInputStream).
    Spero di aver chiarito qualche tuo dubbio!
    Ciao
    Diego

  • About abstract method read() in class InputStream (second message)

    I know but my question needs other arguments. When the Java virtual machine must help me in reading some input stream, I imagine it uses some calls to the operating system: I would like to know if behind that method there is something which seems a system call even if is abstract and so without apparently code.

    Please post this as a "reply" in your original thread
    http://forum.java.sun.com/thread.jspa?threadID=5192979&tstart=0
    Don't start a new thread, simply reply to one of the other posts in the original thread (there is a button "reply" on the right hand side).

  • ByteBuffer: how come the abstract methods work?

    I am confused about the abstract methods in ByteBuffer class. Many of the methods in that class are declared abstract but yet I am able to use them without a problem.
    For example, put(byte b) is declared abstract in the java docs. But I am able to use that method ( I did not have to extend ByteBuffer and implement this method). I know I must be missing something here...any help would be appreciated.
    thanks

    Those responses should certainly do the trick. But I love hearing myself type, so here's an example:abstract class Fred
         public abstract void hiThere();
    class SubFred extends Fred
         public void hiThere() { println("Greetings Professor Falken"); }
    }Somewhere some method says it returns an object of type Fred. To do this they might (as far as you know) create an instance of SubFred and return that to you. SubFred is of type Fred. Your code doesn't know or care (usually) what the ACTUAL class is, as long as it matches what you expect Fred to be from the documentation.

  • Overiding super class method to an abstract  method

    public class Super
    public void doSomethingUseful()
    public abstract class Sub extends Super
    public abstract void doSomethingUseful();
    What is the OO principle behind this?
    When do we need to override a super class method in subclass as an abstract?

    Lets first look at a simple design pattern called "Template Method".
    public abstract class Library
    private void collectBooks()
    // collect books here
    private void putBookInShelf()
    // put books in shelf here
    // abstract method sortBooks()
    public abstract void sortBooks();
    public void processBooks()
    collectBooks();
    sortBooks();
    putBooksInShelf();
    this class is an abstract class giving an abstract method called "sortBooks()", what is it useful for? We can make a subclass and implement sortBooks() to sort the books as we want (title wise, author wise, date wise, publisher wise) and then simply call processBooks() to process them.
    One Sub class may look like:
    public class MyLibrary extends Library
    public void sortBooks()
    // sort books by title b/c I like them sorted out by title
    Another sub-class may look like
    public class HisLibrary extends Library
    public void sortBooks()
    // sort books by Author, b/c he likes his books sorted out by author
    Now client will say:
    public static void main(String str[])
    MyLibrary mylib=new MyLibrary();
    mylib.processBooks(); // books will be processed by sorting them title wise
    HisLibrary hislib=new HisLibrary();
    hislib.processBooks(); // books will be processed by sorting them author wise
    So in Library class, method "sortBooks()" was a template method allowing subclasses to sort the books as they want while all other functionality was implemented by Library class itself.
    Now if we go back to your example, a method which is concrete in super class that you converted into an abstract method in sub class ( doSomethingUseful() ) is now a template method, which alows the sub classes of this subclass to do something useful what they think is useful or in other words you are allowing subclasses of this subclass to implement this template method as they want by using their own algorithm.
    Now why whould you do that? answer is that you don't have access to the code of super class, otherwise you must have made this method abstract in super class in the first place.
    Note that the code may not compile, I tried to come up with an exmple and did not pay attention to compiler demands.
    I think I cleared my point, It was tough to explain though.
    Good Luck.
    Khawar.

  • Parent constructor calls abstract method

    Hi everybody!
    I'm wondering if there is something wrong with java or if the idea is just too ill?
    Anyway, I think it would be great if this hierachy would work...
    Two classes A and B.
    Class A defines an astract method.
    In A's constructor this abstract method is called.
    Class B extends A and provides an implementation for the abstract method in A.
    Class B also defines a member variable that is set in B's implementation of the abstract method.
    In class' B constructor the parent constructor A() is called.
    example:
    public abstract class A {
      public A() {
        createComponents();
      public abstract void createComponents();
    public class B extends A {
      private String string = null;
      public B() {
        super();
        System.out.println("B::B() " + string);
      public void createComponents() {
        System.out.println("B::createComponents() begin");
        string = new String("test");
        System.out.println("B::createComponents() " + string);
      public void describe() {
        System.out.println("B::describe() " + string);
      public static void main(String[] args) {
        B b = new B();
        b.describe();
    }running the code above produces the following output:
    B::createComponents() begin
    B::createComponents() test
    B::B() null
    B::describe() null
    why is the string member variable null in B's constructor??
    thanks in advance
    Peter Bachl
    Polytechnic University of Upper Austria, Hagenberg
    [email protected]

    The answer is that the call of the super-constructor
    is allways done before the initialization
    of the member variable. That's all and that's the
    normal behavior.
    order :
    - initialization of static variables
    - call to the super-constructor
    - initialization of the instance variables
    - execution of the constructor
    Since this is the advanced forum it is relevant to point out that that is not exactly true.
    There is a step in java that 'initializes' member variables before any constructors are called, super or other wise.
    From the JLS 12.5...
    Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (4.5.5)
    Using the following code as an example
      class MyClass
         int i1;
         int i2 = 1;
    .When creating an instance of the above there will be three 'initializations'
    // Part of object creation...
    i1 = 0; // The default value
    i2 = 0; // The default value
    // Part of construction...after super ctors called.
    i2 = 1; // The variable initializer (see step 4 of JLS 12.5)
    Unfortunately the descriptions are rather similar and so confusion can result as to when the assignment actually occurs.

  • Virtual methods- abstract methods

    Hi, I need to get this clear in my head.
    In C++ you had virtual methods()
    If a method was virtual this made the enclosing class abstract.
    There is no such thing as abstract methods in C++?
    In java, if you have an abstract method your enclosing class becomes abstract.
    There is no such thing as a virtual method in java.
    is all the above correct?
    Thanks ... J

    Hi, I need to get this clear in my head.
    In C++ you had virtual methods()
    If a method was virtual this made the enclosing class abstract.
    There is no such thing as abstract methods in C++?Incorrect.
    Abstract methods have no implementation. You can have those in C++. If I remember correctly, they'd look something like this:
    public void foo() = 0;
    In java, if you have an abstract method your
    enclosing class becomes abstract.
    There is no such thing as a virtual method in java.Incorrect.
    All methods in Java are virtual by default. Every method is fair game to be overridden by a subclass. You mark them with the final keyword to prohibit overriding.
    All methods in C++ are final by default. You can't override any method in a subclass unless you mark it with the virtual keyword. (This was keeping the Bjarne's philosophy of "only pay for what you use".)
    %

  • Why java does not force to declare atleast one abstract method

    hi,
    i can define an abstract class without declaring any abstract method in that class. But why wud i do this ? i mean when i have decided that a particular class should be inherited by other subclass and subclass should porvide implementation then there should be atleast one method in the abstract super class which requires implementation.
    All i want to know is why java does not force to declare atleast one abstract method in abstract class.
    there may be some situations where this restriction can create problem if it is like that then can anybody give some example.
    manish

    hi,
    i didn't get u.
    u r trying to say that i have an abstract class with
    only static methods then my questions is why wud
    declare such a class as 'abstract' class? because a
    static method can't be abstract also. Even then if
    somebody want to define such a class with only static
    methods then compiler should force him to declare
    atleast one abstract method which can be implemented
    by subclass, because as i said before if sumbody
    decide to define a class abstract then he wants that
    it should be inhereted but as u r saying a class with
    only static methods then it should not be an abstract
    class it can be a simple class.there's no functional reason, really... actually, factory-like classes are often defined the way Ceci described
    "abstract" only ensures that nobody can ever get an instance of that class (as a matter of fact, what would be the point of getting an instance, if no instance method exists ?)

  • Ramifications of extending subclass by creating abstract method in base

    What r the ramification of extending subclass functionality by creating abstract method in base class?

    javax.swing.JComponent is an example of an abstract
    class with no abstract methods. That is why the
    following works:
    JComponent component = new JComponent(){};
    Not to forget you cannot instantiate abstarct classes
    public abstract class Test
    public String getName()
    return "Mike";
    public static void main(String[] args)
    Test tt = new Test();
    System.out.println(tt.getName());
    }and you would get the error
    The type Test cannot be instantiated.
    You remove the abstract keyword and it would compile
    good.No I am not misinterpreting I know what he is saying but I am closing the door of misinterpretation which I felt was possible. ;)
    cheers

  • Abstract method called in an abstract class

    Hello,
    I am writing some code that I'd like to be as generic as possible.
    I created an abstract class called Chromozome. This abstract class has a protected abstract method called initialize().
    I also created an abstract class called Algorithm which contains a protected ArrayList<Chromozome>.
    I would like to create a non abstract method (called initializePopulation()) which would create instances of Chromozome, call their method initialize() and full the ArrayList with them.
    In a practical matter, only subclass of Algorithm will be used, using an ArrayList of a subclass of Chromozome implementing their own version of initialize.
    I have been thinking of that and concluded it was impossible to do. But I'd like to ask more talented peaple before forgetting it !
    Thanks,
    Vincent

    Ok, let's it is not impossible, juste that I had no idea of how doing it :-)
    The difficulty is that Algorithm will never have to deal with Chromozome itself, but always with subclass of Chromozome. This is usually not an issue, but in that case, Algorithm is required to create instances of the desired subclass of Chromozome, but without knowing in advance wich subclass will be used (I hope what I say makes any sense).
    Actually I may have found a way in the meantime, but maybe not the best one.
    I created in Algorithm an abstract method :
    protected abstract Chromozome createChromozome()The method initializePopulation will call createChromozome instead of calling directly the constructor and the initialize() method of Chromozome.
    Then subclass of Algorithm will implement the method createChromozome using the desired subclass of Chromozome.

  • Can i call non -abstract method in abstract class into a derived class?

    Hi all,
    Is it possible in java to call a non-abstract method in a abstact class from a class derived from it or this is not possible in java.
    The following example will explain this Ques. in detail.
    abstract class A
    void amethod()
    System.out.println(" I am in Base Class");
    public class B extends A
    void amethod()
    System.out.println(" I am in Derived Class");
    public static void main (String args[])
    // How i code this part to call a method amathod() which will print "I am in Base Class
    }

    Ok, if you want to call a non-static method from a
    static method, then you have to provide an object. In
    this case it does not matter whether the method is in
    an abstract base class or whatever. You simply cannot
    (in any object oriented language, including C++ and
    JAVA) call a nonstatic method without providing an
    object, on which you will call the method.
    To my solution with reflection: It also only works,
    if you have an object. And: if you use
    getDeclaredMethod, then invoke should not call B's
    method, but A's. if you would use getMethod, then the
    Method object returned would reflect to B's method.
    The process of resolving overloaded methods is
    performed during the getMethod call, not during the
    invoke (at least AFAIK, please tell me, if I'm wrong).You are wrong....
    class A {
        public void dummy() {
             System.out.println("Dymmy in A");
    class B extends A {
         public void dummy() {
              System.out.println("Dymmy in B");
         public static void main(String[] args) throws Exception {
              A tmp = new B();
              Class clazz = A.class;
              Method method = clazz.getDeclaredMethod("dummy", null);
              method.invoke(tmp, null);
    }Prints:
    Dymmy in B
    /Kaj

  • "Abstract" method in a non-abstract class

    Hi all.
    I have a class "SuperClass" from which other class are extended...
    I'd like to "force" some methods (method1(), method2, ...) to be implemented in the inherited classes.
    I know I can accomplish this just implementing the superclass method body in order to throw an exception when it's directly called:
    void method1(){
    throw new UnsupportedOperationException();
    }...but I was wondering if there's another (better) way...
    It's like I would like to declare some abstract methods in a non-abstract class...
    Any ideas?

    The superclass just models the information held by
    the subclasses.
    The information is taken from the database, by
    accessing the proper table (one for each subclass).??
    What do you mean by "models the information"?
    You should use inheritance (of implementation) only when the class satisfies the following criteria:
    1) "Is a special kind of," not "is a role played by a";
    2) Never needs to transmute to be an object in some other class;
    3) Extends rather than overrides or nullifies superclass;
    4) Does not subclass what is merely a utility class (useful functionality you'd like to reuse); and
    5) Within PD: expresses special kinds of roles, transactions, or things.
    Why are you trying to force these mystery methodsfrom the superclass?
    It's not mandatory for me to do it... I 'd see it
    just like a further way to check that the subclasses
    implements these methods, as they have to do.That's not a good idea. If the superclass has no relation to the database, it shouldn't contain methods (abstract or otherwise) related to database transactions.
    The subclasses are the classes that handle db
    transaction.
    They are designed as a binding to a db table.And how is the superclass designed to handle db transactions? My guess (based on your description) is that it isn't. That should tell you right away that the subclasses should not extend your superclass.

Maybe you are looking for

  • Follow up on Installing OS 9 on a Beige G3

    Hello again, Thanks to XPF, I now have Panther running on my Beige G3. It works fine and it will let me boot to 9.2.2, but when I try to reboot back into Panther it freezes and I'm stuck in 9 Any ideas? Jer

  • How can I sort all my books into title order?

    Hi I'm familiar with how to search for book titles but it would be helpful if I could list all my books in alphabetical order.  Does anyone know how to do this please? Many thanks Rathi

  • Is there a Special Character for the number of pages in a document?

    I'm creating a template (in cs6) for a series of documents of variable length, and each document needs to be numbered with the format, "page A of Z." I see the special character for inserting the current page number (A) and I'd like to have the numbe

  • Can't Change an Image's object placment after inserted

    I am using Pages '08 and each time I insert an image into my document, it either automatically inserts as inline or as floating, however, I am never allowed to change this designation. On the formatting bar, the Object placement options are both gray

  • Re: Problems Running Adobe Pro 11 - error 16

    Hej, neither Adobe's solution (Configuration error 16 | CC, CS)  nor the above modification by Nate worked for me under Mac OS 10.9. I recently updated to Mac OS 10.10 and the error still appears Adobe Reader Pro 11 always crashes after a minute: "Co