• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OOP, what is an Object?

 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To understand why it is so important to differentiate between plans and instances, it is useful to take a brief look at some of the consequences of delivering plans rather than instances.
Plans can be parametrized, applied recursively, scaled, and instantiated any number of times. None of this is possible with actual instances.
As, with software, it is the plans that are delivered, instances can be of different shapes by using different parametrizations of the plan. In other words, software is a generic metaproduct that can be used to create entire families of instances.

Objects are almost never sold, bought, or deployed. The unit of deployment is something rather more static, such as a class, or, more likely, a set or framework of classes, compiled and linked into some package. Objects that logically form parts of a software component “instances” are instantiated as needed, based on the classes that have been deployed with the component(that's another type of animal ). Although a component can be a single class, it is more likely to be a collection of classes, sometimes called a module.However a component could just as well use some totally different implementation technology, such as pure functions or assembly language, and look not at all object-oriented from the inside



meta-something : it's not something but a thing that  somewhat describes something as meta-data meta-class meta-model etc
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still disagree. I think what you are describing is the development process, not the finished product. Though we know there are some people who let the customers finish the development
Of course many software products are sold as tools to make something different; maybe that is what you mean.

Harry Kar wrote:. . . meta-something : it's not something but a thing that  somewhat describes something as meta-data meta-class meta-model etc

That makes you sound somewhat something like Humpty Dumpty in Alice Through the Looking‑Glass.
 
Harry Kar
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I still disagree. I think what you are describing is the development process, not the finished product. Though we know there are some people who let the customers finish the development
Of course many software products are sold as tools to make something different; maybe that is what you mean....



Really I had  in mind sw components  too but till now i was referred  to sw in general. For a point of view (sw professionals) sw is really a finished product(after all is the matter of the profession) but  if you go out of that point of view and consider the wider context what you observe? actually what can do the  sw alone without hw(a computational unit anyway)? when you buy a sw and take it in hand what can you do (autonomously) with that?  conversely when you buy a completely different finished product(autonomous and hence ready for immediate use) as a circular saw, an auto, a piece of cheese etc do the same question notice some difference?
Dunno if am i but for me is intuitive to see the difference  

* sw = software , hw = hardware
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I understand that post, but in the case of the circular saw I mentioned earlier, it needs to be installed. Unless I connect it to a source of energy, it will do nothing. I can only cut wood with it if I provide it with electricity. Nothing we can make exists in isolation. Yes, you must have hardware to run software on. Yes, you mist have a floor to put the circular saw on, and electricity to make it work.
 
Harry Kar
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:... in the case of the circular saw I mentioned earlier, it needs to be installed. Unless I connect it to a source of energy, it will do nothing. I can only cut wood with it if I provide it with electricity. Nothing we can make exists in isolation. Yes, you must have hardware to run software on. Yes, you mist have a floor to put the circular saw on, and electricity to make it work.



It's hard when reasoning with metaphors imho you can't compare a source of energy with a computer(that needs too a source of energy). If we consider all the possible "indirections"( due to the context because nothing we make exists in isolation) in the case of sw we've +1  

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is difficult to understand, partially because of the abbreviations, which will be incomprehensible to some people, but I think we are agreeing with each other now.
 
Harry Kar
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is difficult to understand, partially because of the abbreviations, which will be incomprehensible to some people, but I think we are agreeing with each other now.



I'm glad about that now officially we can call a metaproduct also HumptyDumpty no matters . However the concept is not trivial to grasp moreover if one work in the field

Cheers
Harry
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harry Kar wrote:. . . now officially we can call a metaproduct . . .

No, you are the only person who ever says metaproduct
 
Harry Kar
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maurizio Gasparro wrote:
...
now, I know that is the "main" Method of the Class and then I can create other Methods within or outside the Class
and use them whenever I need them.
That's fine, I've got it! (I think...)



I return on theme because in a previous post speedily i respond you  

Harry Kar wrote: ...you have to decide what class is your main class and put there your public static main() method that is the entry point in a Java
application(and in a C like application too)



To elaborate a bit more is worth to say some more things
Regardless if you know about C, C++ in those languages  each program can have only a single main() function, and people programming in those languages
are no more confused on what the meaning of main() is; they know that the main() method is where every C program starts executing. All that changes in Java.

Is main() the entry point to your application, as it is in C++?
Well..., yes, no, sometimes, and perhaps are alle valid answers.
This confusion arises because there is no division between Java programs and Java objects like there is in languages like C++ or Object Pascal.

In Java
  • Each class in a program/application can have its own main() method.
  • The programmer  may decide to start the program using the main() method of any class in the application.


  • E.g. suppose you have two  classes, the Dancers and the Singers.
    Because you realize that a Java program is essentially a collection/network of cooperating objects, you give each class a main() method, which enables it
    to cooperate with the other class for the purpose of putting on a combined Singers-Dancers performance.

    Lets put down few lines of example code(if possible in that case absolutely on a text editor instead of an IDE ):





    Like the Dancer class, the Singer class has also been designed to be cooperative. Its main() method creates the Dancer object/instance dean,
    and puts him to work, along with a few Singers as well.

    Now let's compile and next if hopefully haven't compile errors  run our programs trough a terminal/console:  
    Open your preferred terminal and go to performance parent's directory/folder(it's best to compile and run the classes from outside the packages)  
    and from there  type:  


    Now is time to execute and get the output
    type:


    Note:
    1.  When you use packages(in real life almost ever you should)
          In the case of java interpreter(execution time) you must use the dot(.) separator between package and class identifiers as opposed to the
           slash(/) separator used at compile time from the Java compiler javac  

    2.    If you take a glance into performance directory you can  notice that  you have two more .class files(other than the two .java class files you had)
          not only one as you probably have expected(because you have compiled only one class (Dancer)) . Means that in compiling  Dancer.java the  
          compiler cleverly saw that Dancer class uses Singer class so recursively compile Singer class too. So it's not necessary for you to compile
          explicitly Singer class.All that because of the connection/relationship between the two classes

    Now let's recap the important  facts here :
  •  Unlike C programs, Java programs can have several main() methods.
  • Even if your class has a main() method, it will never be executed unless that class is invoked by the Java interpreter(java).
    It is easy to confuse the class constructor, which is called every time an object is created, and the main() method, which is only
    called when the class is used to “start-up” a Java program/application.
  • Like C programs, only one main() method will act as the “entry point” or starting position each time your program runs.
    Unlike C programs, however, the main() method that performs this service is determined at runtime, not when you compile
    your program.


  • To keep your Java classes from becoming hopelessly intertwined:
  • Make a clear distinction between application classes and the working classes in your design. An application class is designed to act
    as an entry-point and overall manager for your application.
    Because the Java language does not enforce this distinction, you will have to do so by convention . One such useful convention is to add
    an “App” suffix to all such application classes e.g. EmailApp, WriterApp.
  • For every other (working) class, resist  to use the main() method as a “test bed” to exercise each of the methods and features of the class. Instead,
    you should not writing main() at all as a good habit because the drawbacks outweigh the advantages.
    Test code placed in a main() method is compiled into byte-codes and is delivered along with your finished application, making it larger and subsequently
    more sluggish.
    Furthermore, if you put a test-bed main() method in every class, you cannot prevent users(of your classes) from running your test code, without actually
    commenting out  the main method when testing is complete. (Commenting out refers to the practice of placing comment markers around some part of
    your program that  you wish to remain in the source code, but you don’t want to appear in the byte code.)
    You should write a test-bed program for every class you develop, but keeping the test code outside of your working classes is probably a better strategy.


  • But then there are statements (which in this case are also Methods of a class but let's forget about this for a moment) like these: that are part of the Method body.
    ....



    A method definition describes the steps that your class will perform to accomplish a particular operation(or if you prefer to respond to a receiving message).
    The bodies of a class’s methods are where the real work of an object-oriented program takes place. The body of a method consists of statements.
    You might think of statements as the sentences of the Java language. Each statement, ended by a semicolon, usually performs a single operation
    or calculation. There will be used several kinds of statements to write your Java methods.
  • Declaration Statements : enable you to create local variables using either primitives or (references to) objects. e.g. int  aPrimitiveVariable;  
    Object  aReferenceToObject; etc
  • Method Calls: Object-oriented programs are composed of a network of cooperating objects, so one of your chief ways of getting things done will be
    to ask  other objects to give you a hand to do your work(you delegate part of your work to other objects).
    The general form is : ReceiverObject.message( [arg1 [, arg2 ...]] );
  • Expression Statements: An expression statement is, formally, “a combination of operands and operators, that, when evaluated, produces a
    value.” In real life, that simply means you can write statements that perform some calculation. e.g.  x = x - 50;  
    Expressions and expression statements are not exactly the same thing. An expression statement is a full expression that ends with a semicolon;
    it may include any number of intermediate expressions (sub-expressions). E.g. gere we've two sub-expressions.
    The first, x – 50, uses the operands (data values) x and 50 to produce another value i,e, the result  of  the expression.
    The second expression is the assignment expression, x = <result-from-the-first-expression>.
    If you come from a Pascal or BASIC background, you might object at this point and say, “No, x – 50 is an expression, but the rest is simply an assignment
    statement, saving the value of that expression.”  
    In Java however, as in C and C++, the assignment operator produces a value, just like any other operator, and it can be used inside expressions.
    If you have two integer variables, x and y, and you want to set them both equal to 0, the following expression statement will do so: x = y = 0;
    This statement first evaluates the expression, y = 0.
       Part of the process of evaluating the expression is performing the operation of assignment. Once this operation is completed, the value of the expression is
       calculated.
    With the assignment operator, the value of the assignment expression is the value of the variable after assignment. It is this temporary value (the result of the
    expression) that is assigned to x.  x = y = 0; is equivalent as  (x = (y = 0) );
  • Flow of Control: The last kind of statement that can appear in a method body is the flow-of-control statement. These give your programs the ability to
    make decisions(selection or branching capability) and to keep working on a problem until it’s done(looping capability).
    Creating variables, assigning values, and performing calculations are all important parts of any computer program, but restricting your software to these features
    would mean that every program would do exactly the same thing exactly the same way every time it was run. To provide any sort of “intelligence,” computer programs
    need the two additional abilities of selection/branching and looping.
  •  
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Harry Kar wrote:. . .

    Why is that line there?

    Harry Kar wrote:. . . you have to decide what class is your main class and put there your public static main() method that is the entry point in a Java
    application(and in a C like application too)

    The main method is probably so called for historical reasons because the main() function is used to start a C application.

    To elaborate a bit more is worth to say some more things
    Regardless if you know about C, C++ . . . .

    Most people on this forum don't know C or C++. They are different languages, so for those few who do, your reminder that they are so different is useful

    This confusion . . .

    What confusion?

    there is no division between Java programs and Java objects . . .

    I would dispute that. A program uses objects, and objects use other objects as part of the program. You have shown yourself that starting programs from different classes causes different results. The difference between results demonstrates that they are different programs. If you go back to weakest precondition semantics, you will find that the weakest precondition for one program to produce the postcondition that the screen contains “...twist...” is true and for the other it is false. They are therefore different programs.
    I think your statement doesn't add any clarity to the discussion. Sorry.

  • Each class in a program/application can have its own main() method.
  • The programmer  may decide to start the program using the main() method of any class in the application.


  • E.g. suppose you have two  classes, the Dancers and the Singers.

    As described earlier, those are different programs.

    . . . Lets put down few lines of example code . . .
    Even if your class has a main() method, it will never be executed unless that class is invoked by the Java interpreter(java). It is easy to confuse the class constructor, which is called every time an object is created, and the main() method, which is only
    called when the class is used to “start-up” a Java program/application. . . .

    That is not actually true; it is possible programmatically to call the main() method,

    . . . Make a clear distinction between application classes and the working classes in your design. An application class is designed to act as an entry-point and overall manager for your application.

    That is potentially a useful concept, but it is not a disctinction most people make. Most people create a single class containing a main() method and probably a private constructor and nothing else. That method is used to start the application and nothing else. It would not usually manage the application. Don't say working class because that can easily be confused with the well‑known category of worker classes.

    . . . enforce this distinction . . . by convention . . . add an “App” suffix to all such application classes . . . .

    Y

    ou are again creating your own terms and conventions, which nobody else uses. There is enough difficulty to being a beginning programmer without people adding confusion with new terminology

    Test code placed in a main() method is compiled into byte-codes and is delivered along with your finished application, making it larger and subsequently more sluggish.

    Larger, maybe, but please justify why you said such code would be sluggish.

    . . . if you put a test-bed main() method in every class, you cannot prevent users(of your classes) from running your test code, without actually commenting out  the main method . . .

    That isn't true; there are much simpler ways to disable main() methods. You are right that modern practice is to have separate classes for doing the testing, but your next sentence now shows your two‑category classification failing: are testing classes application classes or working classes?

    You should write a test-bed program for every class you develop, but keeping the test code outside of your working classes is probably a better strategy.

    . . . The bodies of a class’s methods are where the real work of an object-oriented program takes place. . . .

    Some poeple would disagree, saying that the fields (data) are more important than the methods.

    . . . Declaration Statements[ . . .

    Declarations aren't statements in Java®.

    Expression Statements[/b]: An expression statement is, formally, “a combination of operands and operators, that, when evaluated, produces a
    value.” . . .

    I have told you before to Quote Your Sources. I tried searching for that quote and was led to a flash‑cards website, so that wouldn't give a formal definition of an expression statement. Some of that sort of training websites are full of errors, but I was unable to find out enough to be sure.

     
    Expressions and expression statements are not exactly the same thing . . .

    . . .  but you then go on to describe an expression. System.out.println("Campbell"); is an expression statement in Java®.

    . . . the following expression statement will do so: x = y = 0;

    That isn't an expression statement in Java®.

    . . . selection/branching and looping.

    Don't say branching or looping, which are both implementation details. It is selection, and iteration or recursion. It is easy enough to prove that iteration and recursion are mutually equivalent in terms of their weakest precondition effects.

    I am afraid that post is not helpful; some parts are confusing or misleading. You have shown some poor examples. I think your little Singer and Dancer programs are unfortunate because you are trying to squeeze procedural programs into an object‑oriented mould.
     
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,
    In my opinion:
    Objects are instances of a class. eg.

    TableAndChairs tc1 = new TableAndChairs();

    This means, creating a reference to a class you create, so as to give you access to call up an attribute or property of the class eg.
    to call up the class from another class - new TableAndChairs();
    to call up a method (not main method - new TableAndChairs().tc1();
    to call up a variable - new TableAndChairs().tc1().sum;

    Note:
    new TableAndChairs(); is an instance of a class
    tc1() is calls up a method
    tc1().sum calls up a default or public variable called 'sum'
     
    Marshal
    Posts: 8857
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Charles Nduka wrote:Hi,
    In my opinion:
    Objects are instances of a class. eg.

    TableAndChairs tc1 = new TableAndChairs();

    This means, creating a reference to a class you create, so as to give you access to call up an attribute or property of the class eg.
    to call up the class from another class - new TableAndChairs();
    to call up a method (not main method - new TableAndChairs().tc1();
    to call up a variable - new TableAndChairs().tc1().sum;

    Note:
    new TableAndChairs(); is an instance of a class
    tc1() is calls up a method
    tc1().sum calls up a default or public variable called 'sum'


    This post seems to be confusing for several reasons, including bad practices such as:
    1. Class name TableAndChairs is something you'd want to avoid. Either Table or Chair would be fine perhaps, but not the both.
    2. Method name tc1() is not descriptive, not clear what it is going to return, so better is to avoid such names, however, from the other your saying I can assume it supposed to return a non primitive data type as lately you are going to demonstrate how to access its field directly, which is a bad practice on its own.
    3. Variable sum shouldn't be allowed to access that way. It should be returned (or calculated) by other method, either so called getter or simply method which calculates current sum.
     
    Greenhorn
    Posts: 1
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Every entity is an object and which having state and behavior is an Object.
    state-variables(data members)
    behavior-method  
    Example- take an example of dog
    Dog(Object)
    breed-
    color-
    age-(these are the variables)
    run()-
    sit()-
    sleep()-(these are actions called as behavior)
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch
     
    Ranch Hand
    Posts: 105
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Objects:  If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior.

    If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail, running.

    If you compare the software object with a real-world object, they have very similar characteristics.

    Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.

    So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

    An object is created from a class. In Java, the new keyword is used to create new objects.

    There are three steps when creating an object from a class −

    Declaration − A variable declaration with a variable name with an object type.

    Instantiation − The 'new' keyword is used to create the object.

    Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

    Example:

     
    Harry Kar
    Ranch Hand
    Posts: 393
    9
    Open BSD BSD Debian
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Purvi Barot Right ;) Welcome to Ranch
     
    Ranch Hand
    Posts: 127
    2
    Monad Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    To really understand what objects and classes are, you have to understand what came before OOP. Before OOP, you had data structures and functionality that operated on those data structures and it was the responsibility of the programmer to make sure they applied the correct functions to the appropriate data structures. Before OOP, there was no contract between data and functionality that operated on that data. OOP created a contract(that is enforced many ways) between data structures and functionality that operates on that data.
     
    If you're gonna buy things, buy this thing and I get a fat kickback:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic