• Post Reply Bookmark Topic Watch Topic
  • New Topic

About method  RSS feed

 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why there is no inheritance of methods ?

And why there nesting of methods is not possible In OOP?is that Because of complexity which it may create or any other reason ?


Happy coading
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by inheritance of methods? You typically inherit from a Superclass and override any methods in your subclass.

Nested methods is not supported in the JLS (Java Language Specification). You can however write an inner class inside a method definition. In OOP, functions or methods are not the basic building blocks unlike functional programming (like Scala) where functions are compiled to a class. In such a scenario you can nest a method inside another method.
 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:What do you mean by inheritance of methods? You typically inherit from a Superclass and override any methods in your subclass.



I mean to ask is why can we extend method like class ? Fine about overloading ..But I dont want to overload

For Example :
we write a class of car in which we write a method of color and size .Then we write a class of BMW which will inherit the car class,Then all the properties of car will come to BMW. I hope I am correct till this point

My question is I dont want to inherit all the properties of car I want only color method to be inherited,Is this can be done ? If so How?If now why ?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can't be done because when you say that BMW is a subclass of Car, you are therefore saying that a BMW is a Car. In other words, anywhere that a Car object is expected you can use a BMW object.

So saying that you don't want BMW to inherit all of Car's attributes conflicts with that, because then a BMW couldn't be substituted for a Car.

Which means that when you say you don't want BMW to inherit some of Car's attributes then you are making a design error. In this case subclassing is not the correct design decision. Perhaps composition would work better, but it really isn't possible to speculate about such contrived examples.
 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It can't be done because when you say that BMW is a subclass of Car, you are therefore saying that a BMW is a Car. In other words, anywhere that a Car object is expected you can use a BMW object.

So saying that you don't want BMW to inherit all of Car's attributes conflicts with that, because then a BMW couldn't be substituted for a Car.

Which means that when you say you don't want BMW to inherit some of Car's attributes then you are making a design error. In this case subclassing is not the correct design decision. Perhaps composition would work better, but it really isn't possible to speculate about such contrived examples.


OK lets change a bit in example I know BMW "IS-A" car , so sub class can be done

Now lets change the example only

Let one class be car and one be home

The car class is having the method of color ,accelerator, fuel etc. And every one will agree "Home IS-NOT A car" so we cant inherit car class

But color can be in home .The "HOME color IS-A CAR color " This is true for example Car is with blue color then home can also be of blue color

So Now in home class i need to write the color class again in home and also it will be written in car.This is something which violets the Main rule and definition of OOP concept and JAVA of the i.e reuse concept

I know one solution to this is Interface but we need to implement it again in both class

Please I need comment over this.I may be wrong but i m telling correctly
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreevathsa Ramesh wrote:
OK lets change a bit in example I know BMW "IS-A" car , so sub class can be done

Now lets change the example only

Let one class be car and one be home

The car class is having the method of color ,accelerator, fuel etc. And every one will agree "Home IS-NOT A car" so we cant inherit car class

But color can be in home .The "HOME color IS-A CAR color " This is true for example Car is with blue color then home can also be of blue color

So Now in home class i need to write the color class again in home and also it will be written in car.This is something which violets the Main rule and definition of OOP concept and JAVA of the i.e reuse concept

I know one solution to this is Interface but we need to implement it again in both class

Please I need comment over this.I may be wrong but i m telling correctly



Since there is no relationship between the classes, of course, they should be separately implemented. Now, why is this an issue?

Well, one possible reason, what if implementing color is ridiculously hard? And you don't want two classes with thousands and thousands of lines of code, that are mostly similar? ... In this case, there are a few solutions. One possibility is to create a Color class, that implements all of those details, of the concept of a color. Then both the home and car class HAS-A Color property, and it's implementation is just a few lines of code to initialize it and use it. Or if it is still too complex, then you can create a helper class, with all the common code -- ideally, the Color class can contain all the helper methods too.


Anyway, since I am speculating on why you don't want to implement separately, I hope I guessed your reason why. If not, can you tell us why implementing an interface is an issue?

Henry
 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Anyway, since I am speculating on why you don't want to implement separately, I hope I guessed your reason why. If not, can you tell us why implementing an interface is an issue?

Henry



what is interface ?It is the one where the we will just initialize that method i mean to tell it will showcase there is method of that kind but we are not implementing that method.

So then where we need to implement those ?In the class which implement the interface so we need to write the same code in both the class .

What we can earn from this ?nothing more than few more lines of code..

And creating the class of color make no sense,In the example which I gave may be one of solution but when we take of the big project it wont be a good deal as its block me from extending the main class of my project

Helper class Its Inner class concept, when we go inner class we can do all sorts of thing ,I mean there is no meaning for the private access only .As by using the Reflection we can use that also .java's inner class in like Hacking,I mean its like hacking method

So from today i will tell Java is not the 100% OOP..proudly I can tellI found this.Yes!!!

Any one can put there argument to this.

Happy coding
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreevathsa Ramesh wrote:
what is interface ?It is the one where the we will just initialize that method i mean to tell it will showcase there is method of that kind but we are not implementing that method.

So then where we need to implement those ?In the class which implement the interface so we need to write the same code in both the class .

What we can earn from this ?nothing more than few more lines of code..

And creating the class of color make no sense,In the example which I gave may be one of solution but when we take of the big project it wont be a good deal as its block me from extending the main class of my project

Helper class Its Inner class concept, when we go inner class we can do all sorts of thing ,I mean there is no meaning for the private access only .As by using the Reflection we can use that also .java's inner class in like Hacking,I mean its like hacking method

So from today i will tell Java is not the 100% OOP..proudly I can tellI found this.Yes!!!

Any one can put there argument to this.

Happy coding



Perhaps, some context is missing.... but I have little idea what you are saying.


I think you are saying that because Java has interfaces, it is not an object orient language. Or because it doesn't have an elegant solution that handles this exact case, it is not an object orient language.... if true, then I can't argue it, as I don't have a strong opinion regarding the OO-ness of a language, in either direction.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will address this point though...

Sreevathsa Ramesh wrote:
I mean there is no meaning for the private access only .As by using the Reflection we can use that also .java's inner class in like Hacking,I mean its like hacking method

So from today i will tell Java is not the 100% OOP..proudly I can tellI found this.Yes!!!


Yes, this is a huge problem with popular languages. Popular languages tend to get used a lot. There are lots of applications and libraries. The applications tend to get big. And there are lots of developers working together on the same project -- with lots and lots of code being changed. And of course, there are lots and lots of users depending on the applications (and making constant demands).

As such, there are needs for tools -- debugging, profiling, monitoring, etc. etc. And these tools need to introspect code, including private members of code.

So, back to the point, yes, tools needs to access the private members of classes. And yes, if there isn't a security manager being used, you can "hack" the code.... but it is solving a problem. Arguably, a problem that is nice to have. If reflection didn't exist, then the complaint would probably be that the language doesn't have any good tools...

Henry
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreevathsa Ramesh wrote:
OK lets change a bit in example I know BMW "IS-A" car , so sub class can be done

Now lets change the example only

Let one class be car and one be home


Fine.

Sreevathsa Ramesh wrote:

The car class is having the method of color ,accelerator, fuel etc.



Why? Why are color, fuel, and accelerator methods? They are not verbs.

Sreevathsa Ramesh wrote:
And every one will agree "Home IS-NOT A car" so we cant inherit car class

But color can be in home .The "HOME color IS-A CAR color " This is true for example Car is with blue color then home can also be of blue color


No Home-color is not the same as Car color. Nor should it be.

Sreevathsa Ramesh wrote:
So Now in home class i need to write the color class again in home and also it will be written in car.


No, you don't need to. Even if it was not the Color class that is provided already by the API, we have ways it to do such things very nicely in Java. So nicely that we don't need to write code in more than one place at all.

Sreevathsa Ramesh wrote:
This is something which violets the Main rule and definition of OOP concept and JAVA of the i.e reuse concept


As already stated, I think the above mentioned design is flawed. You might want to come up with something that will really make us question Java's approach to supporting code reuse.

Sreevathsa Ramesh wrote:
I know one solution to this is Interface but we need to implement it again in both class

Please I need comment over this.I may be wrong but i m telling correctly

Again, no you don't need to.

Edit : Added "fuel" in my second line in the list of methods that should rather be attributes.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not have nested methods cause it does not require them.

If you can state an instance where you think nested methods might add value to Java applications, let's discuss that.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreevathsa Ramesh wrote:what is interface ? ...
So then where we need to implement those ?In the class which implement the interface so we need to write the same code in both the class .

My suggestion: read the DontSweatIt page and, in particular, this topic, because it seems to me that you're spending a lot of energy complaining about things that can't be changed.

And creating the class of color make no sense...

It most certainly does. Indeed, Java already has one: java.awt.Color; so, as was already stated, you can easily implement a HAS-A relationship, which allows you to implement pretty much any Coloured object (assuming you have such an interface) in one line of code.

I mean there is no meaning for the private access only .As by using the Reflection we can use that also .java's inner class in like Hacking,I mean its like hacking method

An astute observation, and just one of the MANY reasons that I detest reflection. Also possibly a good reason to incorporate a SecurityManager.

Unless you're writing a generic test utility, or a tool that specifically requires introspection, there should rarely be a reason to use any "reflective" methods other than getClass().

Winston
 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heena Agarwal wrote:
Sreevathsa Ramesh wrote:
OK lets change a bit in example I know BMW "IS-A" car , so sub class can be done

Now lets change the example only

Let one class be car and one be home


Fine.

Sreevathsa Ramesh wrote:

The car class is having the method of color ,accelerator, fuel etc.



Why? Why are color, fuel, and accelerator methods? They are not verbs.

Sreevathsa Ramesh wrote:
And every one will agree "Home IS-NOT A car" so we cant inherit car class

But color can be in home .The "HOME color IS-A CAR color " This is true for example Car is with blue color then home can also be of blue color


No Home-color is not the same as Car color. Nor should it be.

Sreevathsa Ramesh wrote:
So Now in home class i need to write the color class again in home and also it will be written in car.


No, you don't need to. Even if it was not the Color class that is provided already by the API, we have ways it to do such things very nicely in Java. So nicely that we don't need to write code in more than one place at all.

Sreevathsa Ramesh wrote:
This is something which violets the Main rule and definition of OOP concept and JAVA of the i.e reuse concept


As already stated, I think the above mentioned design is flawed. You might want to come up with something that will really make us question Java's approach to supporting code reuse.

Sreevathsa Ramesh wrote:
I know one solution to this is Interface but we need to implement it again in both class

Please I need comment over this.I may be wrong but i m telling correctly

Again, no you don't need to.

Edit : Added "fuel" in my second line in the list of methods that should rather be attributes.


I am not getting what actually your speaking ..just commenting against to all .. what you actually want to say here ?
 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sreevathsa Ramesh wrote:what is interface ? ...
So then where we need to implement those ?In the class which implement the interface so we need to write the same code in both the class .

My suggestion: read the DontSweatIt page and, in particular, this topic, because it seems to me that you're spending a lot of energy complaining about things that can't be changed.


Ya fine it cant be changed as if now I agree with You But one day The oracle people will come with solution as they came with wapper class for primitive types and one more thing you talked about waste of time energy I am so happy doing that .without asking why and how we cant learn anything which others dont know

And creating the class of color make no sense...
It most certainly does. Indeed, Java already has one: java.awt.Color; so, as was already stated, you can easily implement a HAS-A relationship, which allows you to implement pretty much any Coloured object (assuming you have such an interface) in one line of code.


Fine i agree with color which is predefined I have taken color just as an example which came to my mind immediately

 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreevathsa Ramesh wrote:

I am not getting what actually your speaking ..just commenting against to all .. what you actually want to say here ?


No, I was certainly not doing that.

Ok, let's try another approach. So let's take your Car class for example. You said Car has color, an accelerator, and fuel. So why would they be methods.



Of course the above design can be improved. This is just to give you a rough idea of what I was saying.
 
Sreevathsa Ramesh
Ranch Hand
Posts: 40
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolving this topic

see everyone knows "why class cant be protected " For this many will come with answer But for my question i am not getting perfect reason why java developers hadnt given this feature

I am not getting the answer ...every one is giving solution to my example but not for my main question about the inheritance of method...

Happy coding ...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreevathsa Ramesh wrote:Ya fine it cant be changed as if now I agree with You But one day The oracle people will come with solution as they came with wapper class for primitive types and one more thing you talked about waste of time energy I am so happy doing that .without asking why and how we cant learn anything which others dont know

Sounds to me like a knee-jerk reply without reading the links I provided. Ah well...

1. Wrapper classes were not a "solution". For the most part they've been around since Java first came out (I think java.lang.Byte and java.lang.Short were added in Java 1.1), and were included specifically because the language has primitives.
2. There's nothing wrong with intellectual curiosity, but it sounds to me more like you're just having a rant; and that's a waste of our time, not just yours.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!