Objects and Classes

Compiled By Unknown - No Comments
 package corejava;  
 public class Mainclass {  
     int myage;  
     public Mainclass(String name) {  
         // This constructor has one parameter, name.  
         System.out.println("Name :" + name);  
     }  
     void setAge(int age) {  
         myage = age;  
     }  
     int getAge() {  
         System.out.println("Age :" + myage);  
         return myage;  
     }  
     public static void main(String[] args) {  
         /* Object creation */  
         Mainclass mc = new Mainclass("Ankit");  
         /* Call class method to set age */  
         mc.setAge(24);  
         /* Call another class method to get age */  
         mc.getAge();  
         /* You can access instance variable as follows as well */  
         System.out.println("Fetched Value via object :" + mc.myage);  
     }  
 }  

Objects
Object is the instance of the class which have 'behavior' and 'states' or we can say 'properties' in other words. For example, lion has states like color, breed etc and also have behaviors like-growling, roaring etc.
Class
A class is a template/blue print for defining the behavior and states of the objects.
Java Objects
If you look around, you will find many live example of objects like humans, bikes, cars, dogs etc.
Humans can have many states like name, complexion, nationality etc and the behaviors like talking, walking etc.
Java objects have very similar characteristics like real word objects having a state and behavior. A java object's behavior is shown via methods and it's state is stored in fields.
Object to object communication is done through methods and methods operates on object's internal state.
Java Classes
The classes in java act like blue print for making objects

Tags:

No Comment to " Objects and Classes "