Trying to understand what a Method is, how can it be 'part of a class'?
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
|
show 2 more comments
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 '18 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 '18 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 '18 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't domy_lambda()
, onlymy_lambda.call
ormy_lambda.()
or one of 50 other ways)
– Sergio Tulentsev
Nov 21 '18 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 '18 at 16:18
|
show 2 more comments
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
ruby methods
asked Nov 21 '18 at 14:15
JulesJules
558
558
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 '18 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 '18 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 '18 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't domy_lambda()
, onlymy_lambda.call
ormy_lambda.()
or one of 50 other ways)
– Sergio Tulentsev
Nov 21 '18 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 '18 at 16:18
|
show 2 more comments
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 '18 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 '18 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 '18 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't domy_lambda()
, onlymy_lambda.call
ormy_lambda.()
or one of 50 other ways)
– Sergio Tulentsev
Nov 21 '18 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 '18 at 16:18
1
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 '18 at 14:16
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 '18 at 14:16
2
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 '18 at 14:22
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 '18 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 '18 at 14:33
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 '18 at 14:33
2
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't do
my_lambda()
, only my_lambda.call
or my_lambda.()
or one of 50 other ways)– Sergio Tulentsev
Nov 21 '18 at 15:04
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't do
my_lambda()
, only my_lambda.call
or my_lambda.()
or one of 50 other ways)– Sergio Tulentsev
Nov 21 '18 at 15:04
2
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 '18 at 16:18
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 '18 at 16:18
|
show 2 more comments
3 Answers
3
active
oldest
votes
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
add a comment |
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
add a comment |
In OOP:
Class is like a blueprint/template. It has properties, methods etc. An object can be created with it. So an object can call method in class.
Usually a method is created to perform an operation.
Example:
// a demo class
public class Animal{
// a method
public void sound(){
// do something...
}
// main class, we create an object and call sound() method
public static void main(String args){
Animal dog = new Animal(); // create an object so that we can use the method
dog.sound(); // method call
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414029%2ftrying-to-understand-what-a-method-is-how-can-it-be-part-of-a-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
add a comment |
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
add a comment |
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
answered Nov 21 '18 at 14:49
MarkMark
1,9861723
1,9861723
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
add a comment |
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 '18 at 15:49
2
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 '18 at 16:52
add a comment |
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
add a comment |
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
add a comment |
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
answered Nov 21 '18 at 14:30
Dustin RDustin R
87112
87112
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
add a comment |
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 '18 at 15:54
2
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 '18 at 16:34
add a comment |
In OOP:
Class is like a blueprint/template. It has properties, methods etc. An object can be created with it. So an object can call method in class.
Usually a method is created to perform an operation.
Example:
// a demo class
public class Animal{
// a method
public void sound(){
// do something...
}
// main class, we create an object and call sound() method
public static void main(String args){
Animal dog = new Animal(); // create an object so that we can use the method
dog.sound(); // method call
}
}
add a comment |
In OOP:
Class is like a blueprint/template. It has properties, methods etc. An object can be created with it. So an object can call method in class.
Usually a method is created to perform an operation.
Example:
// a demo class
public class Animal{
// a method
public void sound(){
// do something...
}
// main class, we create an object and call sound() method
public static void main(String args){
Animal dog = new Animal(); // create an object so that we can use the method
dog.sound(); // method call
}
}
add a comment |
In OOP:
Class is like a blueprint/template. It has properties, methods etc. An object can be created with it. So an object can call method in class.
Usually a method is created to perform an operation.
Example:
// a demo class
public class Animal{
// a method
public void sound(){
// do something...
}
// main class, we create an object and call sound() method
public static void main(String args){
Animal dog = new Animal(); // create an object so that we can use the method
dog.sound(); // method call
}
}
In OOP:
Class is like a blueprint/template. It has properties, methods etc. An object can be created with it. So an object can call method in class.
Usually a method is created to perform an operation.
Example:
// a demo class
public class Animal{
// a method
public void sound(){
// do something...
}
// main class, we create an object and call sound() method
public static void main(String args){
Animal dog = new Animal(); // create an object so that we can use the method
dog.sound(); // method call
}
}
edited Nov 28 '18 at 21:46
answered Nov 28 '18 at 21:25
Pu ChenPu Chen
356
356
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414029%2ftrying-to-understand-what-a-method-is-how-can-it-be-part-of-a-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 '18 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 '18 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 '18 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't do
my_lambda()
, onlymy_lambda.call
ormy_lambda.()
or one of 50 other ways)– Sergio Tulentsev
Nov 21 '18 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 '18 at 16:18