Core Java - Objects and ClassJava is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts: In this chapter, we will look into the concepts Classes and Objects and methods. Objects in Java:Let'd discuss what are objects.For example we can consider the real-world where we can find many objects, Dogs, Humans, Bikes, Cars etc. All these objects have a state and behavior. If we consider a Bike, then its state is - name, company, color,price and the behavior is - starting, running,stopping.Classes in Java:A class is a template/blue print from which individual objects are created. A sample of a class is given below:Eg:
public class Bike{
//states
String name;
String company;
String color;
double price;
//behaviours
void starting(){
//stmnts
}
void running(){
//stmnts
}
void stopping(){
//stmnts
}
}
A class can contains the following variable types of
Constructors:When discussing about classes, one of the most important subtopic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class the Java compiler will defaultly build a constructor called as default constructor for that class.Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor. Similarly, like methods we have two types of constructors are 1)No-argument constructor 2)Parameterized constructor Note:It will not have modifier and return type. Eg:
public class Songs{
public Songs(){
//This constructor is called as Default constructor.
}
public Songs(String name){
// This constructor is called as Parameterized constructor.
}
}
Creating an Object:As we mentioned before, class provides the blueprint for objects. So normally an object is created from a class. In Java the new keyword is used to create new objects. There are three steps for creating an object from a class:
public class Songs{
public Songs(String name){
// This constructor is called as Parameterized constructor.
System.out.println("Genre of the song is :"+ name );
}
public static void main(String[]args){
// Following statement would create an object myPuppy
Songs ref =new Songs("melody songs...");
}
}
Output:Genre of the song is :melody songs... Accessing Instance Variables and Methods:Instance variables and methods are accessed via created objects. To access an instance variable the fully qualified path should be as follows:
/* Creating object */
ObjectReference = new Constructor();
/* Calling a variable */
ObjectReference.variableName;
/* Calling a class method */
ObjectReference.MethodName();
Eg:
public class Songs{
String name;
public Songs(String name){
System.out.println("Genre of the song is : "+ name );
}
public void setname(String name ){
this.name = name;
}
public String getname(){
System.out.println("Year of song is : "+ name );
return name;
}
public static void main(String[]args){
Songs ref =new Songs("melody songs...");
/*Calling a class method to set genre of song */
ref.setname("1990's");
/* Calling a class method to get genre of song */
ref.getname();
/* You can access instance variable as follows as well */
System.out.println("Variable Value : "+ ref.name );
}
}
Output:Genre of the song is : melody songs... Year of song is : 1990's Variable Value : 1990's ☛ Join to Learn from Experts: Java / J2EE Training in Chennai by TesDBAcademy
« Previous
Next Topic »
(Core Java - Datatypes) |