Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Inheritance

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inheritance

    Hi I'm sort of a beginner in java, which explains some of the simple questions i may ask...
    My question now is: I'm making a small animation with java using Inheritance and methods. I've extended one of my classes from JPanel (public class Boy extends JPanel) in order to use g.fillArc to make a half circle. However, i want to extend this class (Boy) from my Main parent class (along with other moving object classes) so that i can write my main program and control the children classes. I know that i cant extend class Boy from JPanel and from Main at the same time. Is there another way to do this so that it will still inherit attributes from Main?
    Thank you!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Inheritance

    There are many ways to accomplish what you are after, that being said you cannot extend a class from two parent classes (java allows multiple inheritance through interfaces however). One way is to have 'Boy' extend Main, and this can have a draw method that accepts a Graphics object. Pass this object to a new JPanel, whose paintComponent method calls the draw method
    In semi-pseudo-code:
    public abstract class MyMain{
        public abstract void draw(Graphics g);
    }
    public class MyClass extends MyMain{
     
        @Override
        public void draw(Graphics g){
            //draw here
        }
    }
     
    public class MyPanel extends JPanel{
        private final MyMain myMain;
     
        public MyPanel(MyMain m){
            myMain = m;
        }
        @Override
        public void paintComponent(Graphics g){
         ////do painting
         myMain.draw(g);
        }
    }

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance

    Inheritance means to acquire property of others. Using the property of other class ans functions.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Inheritance

    Quote Originally Posted by samualrich123 View Post
    Inheritance means to acquire property of others. Using the property of other class ans functions.
    Not really, no. If you're going to be this vague with your definitions, you'll confuse people.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance

    Inheritance means to acquire property of others. It also means that we can use code of other functions and methods. Means Code reusability. In this object are defines by classes that are super class and which classes uses property of that class is known as sub class. It is very useful in Object Oriented Languages.

Similar Threads

  1. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM
  2. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  3. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM

Tags for this Thread