wood burning stoves*
The moose likes Beginning Java and the fly likes difficult to understand oops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difficult to understand oops" Watch "difficult to understand oops" New topic
Author

difficult to understand oops

Anant Dixit
Greenhorn

Joined: Oct 26, 2009
Posts: 13
well friend i think it is very simple question to many of u..

but my concept about opps is not clear even then i can create a good project on java...
but the basic thing that i want to understand the concept of oops which is very essential for any developer..

i want to know how can we conclude that this thing (particular any thing) is going to be a class or object..

one thing more what is the existence of "ourself" in this world?

are we a object or a class..

please help me out so that i can understand the oops concept.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Anant Dixit wrote:
but my concept about opps is not clear even then i can create a good project on java...


i dont believe . we are an Objects . what do you think?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Welcome to JavaRanch, Anant Dixit !


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

Hello and welcome to the Ranch.

In OOP terms, everything "can be" an object. A class is just a representation of the object. If you have done a bit Java projects then you should at least know the concepts of OOP such as encapsulation, inheritance and polymorphism.

IMHO the primary purpose of OOP is reuse. You may want to look through the Java tutorials on OOP here


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Anant Dixit wrote:well friend i think it is very simple question to many of u..

but my concept about opps is not clear even then i can create a good project on java...
but the basic thing that i want to understand the concept of oops which is very essential for any developer..


Yes, indeed you are very right and in a proper path towards learning the basics! Appreciate that!


i want to know how can we conclude that this thing (particular any thing) is going to be a class or object..


There is a *big* difference between the two. Class is a template or blueprint with which the objects are prepared. You can take Class as an showpiece or model and the object is the *real product*.

The other difference is, "object" is a 'real', 'live' entity with manageable properties and methods to operate upon!


one thing more what is the existence of "ourself" in this world?

are we a object or a class..


I just read it as a single line (only first line) and got confused what to say? lol!!

We would be the 'object' of the 'Person' class! -- does it make sense?

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

K. Tsang wrote: A class is just a representation of the object.


Not true! A class is a template or a bare bone structure based on which the objects are made. You can even call it as a rough figure!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Consider the following analogy: A class is a blueprint to make objects. For example, suppose you have a class House. The class House is the blueprint that explains exactly how to make a specific House object.

Your own house, or your sister's house, are instances of class House - they are the concrete House objects that are made using the blueprint.

So, a class is like a recipe, or a set of instructions, or a plan, that explains how to make a specific kind of object.

See Trail: Learning the Java Language in Sun's Java Tutorials. There are lessons there about object-oriented programming concepts and classes and objects.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Well, adding on top of Jesper's explanation, you can think of a class as a 'Reference' for making the 'real' objects!

Every time you look at the reference/guide before you create an object in existence as it serves you the basic information of

1. What actually constitutes an object? Other way, what will the object be having with it? If it is a House object, it would be having doors, windows etc., If it is a Person object, it would be having eyes,ears, nose etc., -- They are called as 'members' of the class (or an object really)

2. They also convey the quantity/volume -- how many doors, windows a 'house' object should have? --- represented by the 'datatypes' like String, int etc.,

3. What are the objects supposed to do? -- In case of a house object, it does NOT seem to do any real activity except staying/lying as it is. But in case of a person object, it is supposed to do many actions like 'breathe()', 'eat()', 'sleep()', 'speak()', 'run()' etc., --- they are called as 'methods' or 'behaviors'.

All these information are present in the class. So you look at your 'class' or the 'reference' or 'template' or a 'blueprint' each time before you create an object.

Moreover, being a 'reference' a class does NOT exist in reality whereas an 'object' is the one which is existing in reality. It is alive!!

Does it help?
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Anant Dixit wrote:
one thing more what is the existence of "ourself" in this world?

are we a object or a class..



A class is a type definition and from it one can create many objects of that type. Say you want to define a type called Human. In its simplest form it would look like this,

class Human {
}

From it you could create objects like,

Human me = new Human();
Human you = new Human();
Human someoneElse = new Human();
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Welcome to the Ranch
Anant Dixit wrote:well friend i think it is very simple question to many of u..

. . .
It obviously isn't a simple question to you; I hope the answers here have been helpful.

But please avoid that sort of abbreviation (u); as explained here, that can cause confusion.
Anant Dixit
Greenhorn

Joined: Oct 26, 2009
Posts: 13
thanks for all of you...

actually you all guys really encourages me coz i think i just asked a very simple question and no one will give me reply..

NOW COME TO THE POINT.

whatever you told me i am accepting..
but one question again come to my mind "What is the existence of ourself in this world?"
as many of you told that we are a object of a human class i also accepted it but now i have a question which really make me thinking again that are we really an object or a class?

class LivingBeing
{

}

class Human extends LivingBeing
{
abstract wayoftalking()
{
}

abstract personality()
{
}

}

class Male extends Human
{

}


now question arises how can we further inherit these class?

coz if i am an object then i should have a object of Male class right?

object Anant=new Male();

but here my question arises if i have a method the wayoftalking(), personality()

then every person has a unique wayoftalking(),personality() so where should i impliment this method?

because the personality of mine is completely unique and no one has that personality.

thats why i m thinking that we are a class.

means

class Anant extends male
{
}

now here i can implement this methods

please help me out from this confusion.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Anant Dixit wrote:
but one question again come to my mind "What is the existence of ourself in this world?"
as many of you told that we are a object of a human class i also accepted it but now i have a question which really make me thinking again that are we really an object or a class?


Java doesn't give you any answers to design questions. It cannot tell you "What is the existence of ourself in this world?". The answer to that question is NOT somehow hidden within the Java language. It's up to you to find the answer and tell Java. You're the designer. You're the programmer.

Java just makes available programming language elements. One is the ability to create your own types using class, enum and interface. Another is the ability to create objects from concrete types. Once you've come up with the "The existence of ourself in this world" design, you go ahead and implement it in Java using the features Java puts at your disposal.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Anant Dixit wrote:...


now question arises how can we further inherit these class?
...
but here my question arises if i have a method the wayoftalking(), personality()

then every person has a unique wayoftalking(),personality() so where should i impliment this method?


Well, here I would look at the key word "every person has a ..." The word 'has' leads to a special relationship. First thing to notice that if an Object HAS something, that something is probably a Thing (and therefore should be an Object). Methods are usually used to perform actions, while Object are used to model Things. And so having a method called personality() probably doesn't make sense. Rather, you should have a Class called Personality (made up of parts which can be used to describe an individual's personality). Then each person might have a Personality Object:


This pushes the problem of 'every person has a unique personality' question out of the Human class and into the Personality class. Now you have to ask the question 'What is a personality' (see the link above) and develop a class which can capture the characteristics of the personality by definition but also capture the uniqueness by varying the components or parts of the Personality.


Steve
Anant Dixit
Greenhorn

Joined: Oct 26, 2009
Posts: 13
thanks Steve

your explanation really help me to understand the misconception that i had.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

well,
let me leave the philosophical aspect and jump to coding.....

This is wrong (at least the compiler would say so..)


1. Human has a few abstract methods, so it should be an abstract class and the methods, they do not need to be defined at all
2. If Male extends Human then
a. Male should either provide implementations for those methods OR
b. Itself be called abstract.

I would Personally favor the second option above (viz 2 > a )

So,

When you say:


or let me state that a bit better:



it would still not compile unless you write:



IF Male was not abstract,

The above Code would still work and It would still prefer Anant's style of walking and Anant's Personality since it overides its Parent

So yes, Java does not provide all the solutions to Live (else i would have had 1000 Girlfriends)

But It does tell you the rules
Man the compiler is strict !!


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

One thing about Java that I had once found a lot confusing at beginning and i feel like sharing:

Pardon my analogy

[Edit: changed to lower caps]

The child is more powerful than the parent
because:
1. He has his own abilities
2. And he has his parent's abilities too

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

salvin francis wrote:... CHILD ... PARENT ...

It is really confusing to think of a superclass as the "parent" and a subclass as the "child", because inheritance in object oriented programming has a totally different meaning than inheritance in biology. Using terms such as "parent class" and "child class" confuses the two and makes it harder to understand what inheritance in object oriented programming means.

Inheritance in OOP means specialization - a subclass is a specialized version of a superclass. Inheritance also implies that there is an is a relationship between the subclass and the superclass - an instance of the subclass is a (specialized version of) an instance of the superclass.

For example, a Male is a specific kind of Human, so it makes sense to make Male a subclass of Human. Note that a Male is a Human.

This doesn't hold for the words "parent" and "child" - a child is not a parent, and it's certainly not a specialized kind of parent.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Salvin Francis, please look at this FAQ.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Campbell Ritchie wrote:Salvin Francis, please look at this FAQ.

sorry, i edited the post now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Apology accepted and thank you
Anant Dixit
Greenhorn

Joined: Oct 26, 2009
Posts: 13
salvin francis wrote:




so when you create a object anant at a same time you implement both the method along with this object.
it means that now Male class is not abstract coz as both the method are implemented here.
but if a also create an object in the same class like


and here i do not implement the methods.

this code is compile coz now Male class already implement both the method

and now if i called the method


then as i think it will show the wayoftalking of anant?

so is it right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
I have already asked you to avoid abbreviations Anant Dixit; many people who didn't grow up speaking English may not know what "coz" means.
Anant Dixit
Greenhorn

Joined: Oct 26, 2009
Posts: 13
Campbell Ritchie wrote:I have already asked you to avoid abbreviations Anant Dixit; many people who didn't grow up speaking English may not know what "coz" means.


sorry i will keep this in mind... and try to avoid these words....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Apology accepted
 
 
subject: difficult to understand oops