Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Creating a class that extends more than one class.

Creating a class that extends more than one class.

Scheduled Pinned Locked Moved Java
c++javalearning
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bill Moo
    wrote on last edited by
    #1

    Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java. So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.

    -- Bill

    N P P M G 5 Replies Last reply
    0
    • B Bill Moo

      Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java. So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.

      -- Bill

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      You need to impliment interfaces rather then extend classes. A single class can only extend one super class, but you can implement as many interfaces as you like. The difficulty here is that you need to code every method:

      class Top {
      void doSomething() {
      // something is done
      }
      }

      interface First {
      void goFirst();
      }

      interface Last{
      void goLast();
      }

      class Bottom
      extend Top
      implement First,
      Second {

      void doSomething() {
      this.goFirst();
      super.doSomething();
      this.goLast();
      }

      void goFirst() {
      // the first bit;
      }
      void goLast() {
      // the last bit;
      }

      }

      Simples!


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      B 1 Reply Last reply
      0
      • B Bill Moo

        Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java. So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.

        -- Bill

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        The "Java way" is to use interfaces to achieve (most of) multiple inheritance. AFAIK, the "Observable/Observer" concept has been pretty much obsoleted by events and actions, so maybe you don't need to do what you can't. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        B 1 Reply Last reply
        0
        • N Nagy Vilmos

          You need to impliment interfaces rather then extend classes. A single class can only extend one super class, but you can implement as many interfaces as you like. The difficulty here is that you need to code every method:

          class Top {
          void doSomething() {
          // something is done
          }
          }

          interface First {
          void goFirst();
          }

          interface Last{
          void goLast();
          }

          class Bottom
          extend Top
          implement First,
          Second {

          void doSomething() {
          this.goFirst();
          super.doSomething();
          this.goLast();
          }

          void goFirst() {
          // the first bit;
          }
          void goLast() {
          // the last bit;
          }

          }

          Simples!


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          B Offline
          B Offline
          Bill Moo
          wrote on last edited by
          #4

          Thanks for that, I'll do some reading on the matter.

          -- Bill

          1 Reply Last reply
          0
          • P Peter_in_2780

            The "Java way" is to use interfaces to achieve (most of) multiple inheritance. AFAIK, the "Observable/Observer" concept has been pretty much obsoleted by events and actions, so maybe you don't need to do what you can't. Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            B Offline
            B Offline
            Bill Moo
            wrote on last edited by
            #5

            Where do you read this? I got the Observable/Observer from the "Java - The complete reference 8th Ed. Ch 18, p541" and there is no reference to it being obsolete or as a candidate for it either. What I want to do is create a class that extends AbstractAction and implements runnable. The actionPerformed will display a file open dialog allowing the user to select N files, these files are then stored in a File[] and the thread started when the dialog is closed to process the files. But I wanted the observable so I could 'emit' messages to the Observer (my UI) so I could say "Processing filename.ext..." as the processing can take a bit of time, hence the use of the thread and 'status message'. But of course if there is a better way of doing this then I'm all for learning. The Events / Actions sounds sensible, are you able to give me a link to something?

            -- Bill

            P 1 Reply Last reply
            0
            • B Bill Moo

              Where do you read this? I got the Observable/Observer from the "Java - The complete reference 8th Ed. Ch 18, p541" and there is no reference to it being obsolete or as a candidate for it either. What I want to do is create a class that extends AbstractAction and implements runnable. The actionPerformed will display a file open dialog allowing the user to select N files, these files are then stored in a File[] and the thread started when the dialog is closed to process the files. But I wanted the observable so I could 'emit' messages to the Observer (my UI) so I could say "Processing filename.ext..." as the processing can take a bit of time, hence the use of the thread and 'status message'. But of course if there is a better way of doing this then I'm all for learning. The Events / Actions sounds sensible, are you able to give me a link to something?

              -- Bill

              P Offline
              P Offline
              Peter_in_2780
              wrote on last edited by
              #6

              :-O That's what I get for trying to make sense at midnight. I use "Java in a Nutshell, 5th ed". Observable/Observer are since Java 1.0 and the comment is "The Observable class and Observer interface are not commonly used. Most applications prefer the event-based notification model defined by the JavaBeans componenent framework and by the EventObject class and EventListener interface of this package. [java.util]" So I misread preference as superceding... I do remember the Event model being introduced (in Java 1.1) in response to a perceived hole in functionality. Looks like you want to make your UI implement EventListener and have the worker thread spit out (your subclass of) EventObjects as required. I'm sure there are plenty of examples floating around the web. (Java's like that :) ) I'd start with Mr G and something like "Java EventObject". Good hunting, Peter

              Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

              1 Reply Last reply
              0
              • B Bill Moo

                Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java. So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.

                -- Bill

                P Offline
                P Offline
                pasztorpisti
                wrote on last edited by
                #7

                Before starting any serious java tutorials its worth reading through the official beginner java tutorial: http://docs.oracle.com/javase/tutorial/index.html[^] Read at least the "Trails Covering the Basics" paragraph and all of its subpages. Its a novice coder tutorial so you will progress with that quickly and you can gain a lot of important java-specific info from it that makes your work much easier. It worth at least a quicky reading even if you are lazy to try the examples!

                1 Reply Last reply
                0
                • B Bill Moo

                  Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java. So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.

                  -- Bill

                  M Offline
                  M Offline
                  Monster Maker
                  wrote on last edited by
                  #8

                  In java, multiple inheritance concept can only be accomplished through interfaces. I do it using the concept of adapter classes.

                  interface Dog
                  {
                  public void bark();
                  }

                  interface Cat
                  {
                  public void meow();
                  }

                  class DogCat implements Dog,Cat
                  {
                  public void bark()
                  {
                  System.out.println("Barking");
                  }

                  public void meow()
                  {
                  	System.out.println("Meowww");
                  }
                  

                  }

                  public class Hybrid extends DogCat
                  {
                  public static void main(String[] args)
                  {
                  Hybrid r =new Hybrid();
                  r.bark();
                  r.meow();
                  }

                  }

                  1 Reply Last reply
                  0
                  • B Bill Moo

                    Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java. So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.

                    -- Bill

                    G Offline
                    G Offline
                    Gowtham Gutha
                    wrote on last edited by
                    #9

                    Multiple inheritance for classes is however not supported by Java unfortunately. It is limited for interfaces only. Alternatively, you can use multi level inheritance that gets the same as the features of the multiple ones. Here is a simple trick that you can do to achieve this.. Let the super classes be S1,S2 class A extends S1 { } class B extends S2 { } class C { // write the code here by creating objects for A,B // This could be good if S1,S2 are abstract and that // you have implemented the abstract methods in the // classes A,B. So you can create objects for A,B // access methods in them (the implemented ones) and // also the normal ones. }

                    Gowtham Gutha (http://java-demos.blogspot.com)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups