-3

In Java, when you pass an object to another object (eg. pass object1 to object2), would object 2 inherit all of object1s properties such as methods and variables, what exactly happens?

tyonne
  • 393
  • 1
  • 3
  • 9
  • 6
    What do you mean by "pass an object to another object" as this is a very ambiguous statement? Please describe in greater detail just what you're trying to do and show some code. – Hovercraft Full Of Eels Jan 10 '15 at 18:20
  • Show an example of exactly what you mean. – Kon Jan 10 '15 at 18:21
  • I think this is headed toward this question: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Jason Sperske Jan 10 '15 at 18:21
  • Inheritance only happens when Class2 extends Class1. – Hovercraft Full Of Eels Jan 10 '15 at 18:21
  • @JasonSperske: I'm not sure. The question is very very vague to me. To the original poster, please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, thereby getting better answers. – Hovercraft Full Of Eels Jan 10 '15 at 18:22
  • For example here, would the view and controller inherit properties from the model? public static void main(String [] args) { Model model = new Model(); Controller controller = new Controller(model); View view = new View(model, controller); } – tyonne Jan 10 '15 at 18:27
  • Please don't post code in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by [editing your question](http://stackoverflow.com/review/suggested-edits/6706828). – Hovercraft Full Of Eels Jan 10 '15 at 18:41

2 Answers2

2

In Java Inheritance is a different concept compared to Passing objects to other object.Actually you cannot pass an object to other object, you can pass object to methods.

Check this To Pass Objects to functions

You can access Instance methods/Instance Variables using Object.

class SuperClass{
   public void method(){
   //....
   //...
   }
}

public class Example{
    public void example(SuperClass object)
    {
        //you cannot directly call super class method
        //if you want to call then use Object
        object.method()
    }
}

In Inheritance you can access Instance Variables/ Methods with out using object to that class.

class SuperClass{
   void method(){
       //....
      //...
   }
}

class Example extends SuperClass{

    public void exampleMethod()
    {
        //You can directly call superclass method here
        method()
    }
}

For more details on Inheritance check

Community
  • 1
  • 1
Manoj Kumar
  • 745
  • 2
  • 8
  • 29
  • Thanks, when you say you can access the object but not all its properties, what what does this mean? What would be the benefit of passing an object to a constructor or method if you can just do inheritance? Thanks – tyonne Jan 10 '15 at 18:41
  • edited answer accordingly.. @tyonne – Manoj Kumar Mar 26 '15 at 12:16
0

IS-A Relationship

Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.

This means you are able to access super class member and method in your subclass.

class A{
   int a;
  }

 class B extends A{

 }

int a get inherited in class B so you can use it in class B Read more

You can not pass one object to another object.

Software objects communicate and interact with each other in two ways:
1. By calling (or invoking) each other's methods
2. By directly accessing their variables.

Read more

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • "class B extends class A" won't compile – Victor2748 Jan 10 '15 at 18:30
  • @Victor2748 ohh edited thanks to highlight my mistake. – atish shimpi Jan 10 '15 at 18:33
  • Thanks. What is the benefit of passing (objects, classes) to a method or constructor if one can use inheritance instead? – tyonne Jan 10 '15 at 18:44
  • If you are trying to acquire the propreties of other object an want to perform operation on that you should use inheritance its best approach. But if you want to pass some values for calculation use method invocation. like to perform addition pass two numbers to method `add(a,b)`, **you can pass (objects) but you cannot pass classes through method.** – atish shimpi Jan 10 '15 at 18:53