filmov
tv
Java programming, class and object concept, create a dog object based on Dog class (template)
![preview_player](https://i.ytimg.com/vi/2inQvCGxFsM/maxresdefault.jpg)
Показать описание
It is a basic unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :
1. State : It is represented by attributes of an object. It also reflects the properties of an object.
2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
3. Identity : It gives a unique name to an object and enables one object to interact with other objects.
Example of an object : dog
Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”.
Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
Example :
As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general,we can’t create objects of an abstract class or an interface.
Dog tuffy;
If we declare reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.
Initializing an object
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.
package ch3cw_dog;
public class Dog {
// declare variable - state, attribute
String name;
String breed;
int age;
String color;
// default constructor
public Dog()
{
name ="Best Friend";
breed ="labrador" ;
age =0;
color = "white";
}
// mutate constructor
public Dog(String n, String b, int a, String c)
{
name = n;
breed = b;
age = a;
color =c;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
public int getAge()
{
return age;
}
public String getColor()
{
return color;
}
public String toString()
{
return("Hi my name is " + getName() + "\nMy breed , age and color are " + getBreed()+" , " + getAge() + " , "+ getColor());
}
}
package ch3cw_dog;
public class UseDog {
public static void main(String[] args) {
Dog defaultDog = new Dog(); // use default value
Dog dogForErber = new Dog("Blackie","Yorkie",2,"Brown");
}
}
1. State : It is represented by attributes of an object. It also reflects the properties of an object.
2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
3. Identity : It gives a unique name to an object and enables one object to interact with other objects.
Example of an object : dog
Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”.
Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
Example :
As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general,we can’t create objects of an abstract class or an interface.
Dog tuffy;
If we declare reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.
Initializing an object
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.
package ch3cw_dog;
public class Dog {
// declare variable - state, attribute
String name;
String breed;
int age;
String color;
// default constructor
public Dog()
{
name ="Best Friend";
breed ="labrador" ;
age =0;
color = "white";
}
// mutate constructor
public Dog(String n, String b, int a, String c)
{
name = n;
breed = b;
age = a;
color =c;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
public int getAge()
{
return age;
}
public String getColor()
{
return color;
}
public String toString()
{
return("Hi my name is " + getName() + "\nMy breed , age and color are " + getBreed()+" , " + getAge() + " , "+ getColor());
}
}
package ch3cw_dog;
public class UseDog {
public static void main(String[] args) {
Dog defaultDog = new Dog(); // use default value
Dog dogForErber = new Dog("Blackie","Yorkie",2,"Brown");
}
}
Комментарии