Results 1 to 6 of 6

Thread: Problem with inheritance in java

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Problem with inheritance in java

    Hello,
    I have a small problem with the code

    1 The application package contains two pacInterface1 and pacObject1. The first is composed of interface and the second classes that implement these interfaces. Thus we will find one class per interface.

    Application 2 (the one that interests me) contains two such packages pacInterface2 and pacObject2.

    Here is what I have:
    Code:
    ObjectPack2 implements InterfacePack2 {
    	Public ObjectPack1 obj;
    
    	Public objectPack2 (obj obj ) {
           		 great(obj );
          		  this. obj  = ObjectPack1;
     	   }
    	
    }
    Here, it is not logical that there is an aggregation and a legacy at the same time, it will turn one or the other. What do you think I turns aggregation or inheritance? Any help on this is appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Problem with inheritance in java

    Hello,
    There are several good solutions. Here if ObjectPack2 and ObjectPack1 implemented the interface functions InterfacePack1 In the same way it would be logical to remove the inheritance since there will be a redundancy and you create an instance of ObjectPack1 in ObjectPack2 or you can merge the 2 interfaces InterfacePack1 and InterfacePack2and 2 classes ObjectPack2 and ObjectPack1. If they implement the functions InterfacePack1 so different, so there you can do that. I hope this information will help you. Thanks in advance.

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: Problem with inheritance in java

    Hello,
    Even i am new to java, but I think I have a solution for this, see if it works for you. The trick here is that, by this heritage in my application2, I think you are forced to re-implement methods interfacePack1 because of your need in the program.
    Code:
    Public void setX(int x0){
    }
    or
    Code:
    Public int getX(){
      return 0;
    }
    There may be different solutions for this, but I now this one and I think this should work fine with you.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Problem with inheritance in java

    Hello,
    To avoid re-implement the interface methods must be 1 anyway remove inheritance. Ideally I would like to do this:

    - Create an abstract class that contains methods common to ObjectPack1 and ObjectPack2,

    - Make these two classes inherit from the abstract class (possibly by overloading the methods of the class mother)

    - Implement concrete classes by their corresponding interface.

    Hope the above points will interest you and you will implement it in your code.

  5. #5
    Join Date
    Dec 2009
    Posts
    292

    Re: Problem with inheritance in java

    Hello,
    The abstract class that I created in application 1, makes the application 1 is an application common to many applications including the 2 is like a kind of interface the different applications, so I could not touch them as this will impact other applications that are managed by others. If you have any other solution for this then it would be great and I can try that solution in my code. If you have any one then please let me know.

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Problem with inheritance in java

    Hello,
    The abstract class is suitable for your application, so created is rather in the 2. But the fact of creation in 1 will not affect the application 1, at worst she will do her nothing. But you never know, maybe others in need. Up to you. I recommend you do to some study on the object oriented programming basic that is about the inheritance , the polymorphism and so on. That is definitely going to help you.

Similar Threads

  1. how to use multiple inheritance in core java
    By Mewad in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:31 AM
  2. What does Inheritance mean in java?
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 11:45 AM
  3. Various forms of inheritance in java
    By Jabeen in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 09:03 AM
  4. Replies: 3
    Last Post: 19-11-2009, 09:24 AM
  5. Multiple Inheritance in Java
    By KALINDA in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 07:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,133,851.13582 seconds with 17 queries