Java is an Object Oriented Programming language. OOP is the core of any programming language according to my point of view. So as a beginning of Object Oriented Programming I will explain classes here. Just understand them simply. Don't worry about classes and objects as soon as their names heard to you.

In OOP, there are four main concepts. They are;
  1. Encapsulation
  2. Polumorphism
  3. Inheritence
  4. Abstraction
All these concepts are described and used using java classes. During this JAVA article series I will explain them future...

What is a class?

A class is a blueprint from which individual objects are created. It includes Attributes and Methods.  
Attributes means the properties, features that a class has. Methods means the behaviors that a class can have or the activities it can do, simply. I can explain this to you using an example.

Example:
Consider a class call Student. All the students belong to this class. What are the attributes it has? Features? A student has a name, age, gender and etc..
What are the methods of this Student class? What can a student do? They may be learn, eat, sleep and etc...
You get the idea?
We can create objects using a class.

What is an object?

Objects are the instances of classes. They are created in the Heap of computer memory(used for dynamic memory allocation) while variables are created in the Stack of the memory(used for static memory allocation).

Objects can perform the actions that its class has and can access the attributes defined in the class.
If I say it like this, Class is the Student..Object is the s1; also a student. This s1 can do the actions and has the features that included in the Student class. That's the simplest explanation. 

SYNTAX

How to declare a class with attributes and methods?

By convention, the first letter of a class's name is in uppercase and subsequent characters are in lowercase (Ex: Student). 

class Student{
 int age = 23;
 void eat() {
  System.out.println("I'm Eating...");
 } 
 void sleep() {
  System.out.println("I'm Sleeping...");
 }
}

How to create an object?

Student s1 = new Student();

How to call a method using this object?

s1.eat();
s1.sleep();

How to run this program finally? Create a java file called Demo. Then enter this code and see the results after compiling and running. We have to create a another class called Demo to run the main method. Then call the methods and attributes using s1 object.

class Student{
 int age = 23;
 void eat() {
  System.out.println("I'm Eating...");
 } 
 void sleep() {
  System.out.println("I'm Sleeping...");
 }
}

class Demo{
 public static void main(String args[]) {
  // create the object
  Student s1 = new Student();
  // call methods
  s1.eat();
  s1.sleep();
  // call attributes
  System.out.println("Student's age is "+s1.age);
  
 }
}

Output:

Now you have the basic idea to create a class and object using it. There's no need of worrying about these OOP topics. You need to understand clearly. That's all. In my case, I will try to give you everything I know and hope you will get the maximum advantage.

Good Luck guys!




0 Comments