CLASS : A Class is a specification/blueprint/prototype/template of Object
Specification : Requirement of an object i.e specifying or describe about something.
Prototype : Non functional form of real thing.They will not function like real things but they just show how the object looks.
- Class is just describing about how an object looks.
- It is a collection of data & methods.
- It is not an object, but its used to construct them.It tells the JVM how to make about object of a particular type.
- Java classes consists of attributes & behaviours.
- Attributes: represent the data is unique to an instance of a class. These are non-local variables that are not declared within method bodies.
- Behaviour: are methods that operate on the data to perform useful tasks.
- Class syntax: use the following syntax to declare a class in java contents of someclassname.java
[public][abstract/final] class someclassname [extends some parent class][implements some interface]
{
// variable & methods are declared within curly braces
}
Here is an example of a HorseClass.Horse is a subclass of mammal & it implements the Hoofed interface.
public class Horse extends Mammal implements Hoofed
{
// horse variables and methods go here
}
Note :
- A class can have public or default(no modifier) visibility.
- It must have the class keyword & class must be followed by a legal identifier.
- It may optionally extend one parent class.By default,it will extend java.lang.Object.
- Each java source file may contain only one public class.A source file may contain any number of default visible classes.
- Finally, the source file name must match the public class name & it must have a .java suffix
OBJECT : Objects are the physical instantiations of classes. They are living entities within a program that has independent lifecycles and that are created according to the class that describes them. Just as many buildings can be built from one blueprint, many objects can be instantiated from one class.
An object is a self-contained bunch of code that knows about itself and can tell other objects about itself if they ask it questions that it understands. An object has data members (variables) and methods, which are the questions it knows how to answer (even though they may not be worded as questions). The set of methods that an object knows how to respond to is its interface. Some methods are open to the public, meaning that another object can call (or invoke) them. That set of methods is known as the object's public interface. When one object invokes a method on an another object, that's known as sending a message, or a message send. That phrase is certainly OO terminology, but most often in the Java world people tend to say, "Call this method" rather than, "Send this message."
- Things an object knows about itself are called Instance variables. They represent an objects state(data) and can have unique values for each object of that type.
- Things an object can do are called Methods that operate on that data.
Conceptual object example:
Suppose we have a Person object. Each Person has a name, an age, a race, and a gender. Each Person also knows how to speak and walk. One Person can ask another Person how old it is, or could tell another Person to start (or stop) walking. In programming terms, you would create a Person object and give it some variables (like name and age). If you created a second Person object, it could ask the first how old it is, or tell it to start walking. It would do those things by calling methods on the first Person object. When we start writing code in the Java language, you'll see how the language implements the concept of an object. Generally, the concept of an object remains the same across the Java language and other OO languages, though it gets implemented differently from language to language. The concepts are universal. Because that's true, OO programmers, regardless of the language they're programming in, tend to speak differently from procedural programmers. Procedural programmers often talk about functions and modules. OO programmers talk about objects, and they often talk about those objects using personal pronouns. It's not uncommon to hear one OO programmer say to another, "This Supervisor object here says to the Employee object, 'Give me your ID,' because he needs it to assign tasks to the Employee." Procedural programmers might think this way of talking is strange, but it's perfectly natural for OO programmers. In their programming world, everything's an object (with some notable exceptions in the Java language), and programs are objects interacting (or "talking") with each other.
0 comments:
Post a Comment