-3

So if I instantiate a class in another class in Java, isn't that inheritance, because I am calling its method?

For example,

public void updateStock(int stockNew){
    stockFinal = stockNew;
    Stock stock = new Stock();
    stock.update(stockFinal);
}

Isn't that inheritance, because I am able to call the method, update().

3
  • 1
    No it's not. I'm not even sure how to answer this question because the problem quite literally just seems to be that you haven't actually read more about inheritance than the name.
    – Cubic
    Apr 22, 2014 at 19:17
  • Inheritance has to do with a class inheriting variables, methods, and constructors. Also, that code probably doesn't do what you think it does. You are instantiating a Stock but not storing it anywhere. You're just doing something to a variable that will be gone when the method ends.
    – nrubin29
    Apr 22, 2014 at 19:19
  • 1
    You need to read a lot more about what inheritance really is... However, if you're talking about calling a subclass method here, then it's a different aspect, but yes, there's a relationship. Speaking deeply into the the different aspect, if Stock's parent has a method called update and you inherited it in Stock then sure, this is called inheriting a method.
    – user1551592
    Apr 22, 2014 at 19:20

7 Answers 7

3

This is inheritance:

 public class Base
 {
     public String Hello() {
          return "hello";
 }

 public class SubClass extends Base
 {
 }

Then it's used like this:

 SubClass sc = new SubClass();
 sc.Hello();   // returns "hello"

or

 Base b = new SubClass();
 b.Hello();  // calls the same as above

Or this, calling the base from an overridden method:

 public class SubClass extends Base
 {
    //@Override
    public String Hello(){
        return super.Hello() + " + override!!!";
    }
 }

The keyword super refers to the classes ancestor.

0

No this is not inheritance. You are just calling the class itself.

2
  • but if I use 'inheritance' I can use its methods. Likewise, if I recall the class, I am able to use its method. So isn't that inheritance? Apr 22, 2014 at 19:17
  • 1
    There is no relationship with calling a method and inheritance. Apr 22, 2014 at 19:18
0

No, that's not inheritance. Inheritance is when a class extends another class. Inheritance is about establishing an is-a relationship, like a Car is a Vehicle. Or a List is a Collection.

In your example, you're just creating a new object or another type and calling a method on it. If you, of type Human, are using a car, of type Car, that doesn't make a Car a Human.

0

No, that isn't inheritance. Inheritance is when a class extends or implements another class. For more information go here

0

Here You can find an explanation of what is OOP and its principles!

0

This is not inheritance. You are just creating a object of a class. You can use method and data member of other class but object of base class can't use method or data member of your class.

0

No, using a class is not the same as inheriting the characteristics of a class.

See the official Java Tutorial to learn more.

3
  • I am using its methods. So whats the difference? Apr 22, 2014 at 19:18
  • @user3445748 If your class inherits from another class, your class provides the same methods that the class you're inheriting from provides. And I'm sure you can understand the difference between "providing" methods and "using" methods.
    – ajb
    Apr 22, 2014 at 19:21
  • @user3445748 I have updated my answer with a link to a Java Tutorial that you might find helpful.
    – cybersam
    Apr 22, 2014 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.