Class & Object in java - Do you know ?????
Hello Friends,
Many people asked me about core java concepts to start their java learning.
This particular blog's posts will focus on basic or core java. We will explore all the basic java concepts one by one along with practical hands on programming.
Introduction -
There are many concepts like Classes, Object, Polymorphism, Inheritance etc. Each concept is important to build basics of programming. Good things about these concepts are that these do not change much across object oriented languages.
Prerequisite -
- Any development IDE - Eclipse/Intellij/Netbeans
- Basic development setup. (Optional)
- Zeal of learning.
What is Class & Objects ?????
Class is like prototype, blueprint for object and Object is real life instance of class. So in simple language we define our object in the form of class.
For example Person. "Person" is logical definition or template. John and Dessy are the real time instance of Person class called Objects.
To understand this in details, lets code and understand. Theory is good but practical gives better understanding so i believe if you know how to implement concept in program then you have better understanding then if you know only theory. We will create same class explained in above diagrom.
Define the class "Person". Template/blueprint.
package com.pojo;
public class Person {
private int unique_id;
private String name;
private int age;
private String city;
private String gender;
public Person() {
}
public Person(int unique_id, String name, int age, String city, String gender) {
this.unique_id = unique_id;
this.name = name;
this.age = age;
this.city = city;
this.gender = gender;
}
public int getUnique_id() {
return unique_id;
}
public void setUnique_id(int unique_id) {
this.unique_id = unique_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void eat(){
System.out.println(this.name +" am eating");
}
public void study(){
System.out.println(this.name +" am studying");
}
public void sleep(){
System.out.println(this.name +" am sleeping");
}
public void play(){
System.out.println(this.name +" am playing");
}
}
Now lets see object creation of person class. (John/Dessy). We can call methods with objects also.
Create one more class for demo. - PolymorphismDemo.java
package com.demo;
import com.pojo.Person;
public class PolymorphismDemo {
public static void main(String[] args) {
Person john = new Person(1, "John", 35, "Delhi","male" );
Person dessy = new Person(2, "Dessy", 20, "Pune","female" );
john.play();
dessy.study();
}
}
Output -
John am playing
Dessy am studying
Process finished with exit code 0
Source code downlaod -
https://github.com/sarveshoza/PolymorphismInJava/
Bonus-
Class and Objects are explained and understand in multiple way but if we know this then it will easy to understand next concepts which is polymorphism
Polymorphism -
Upcoming Posts -
2. Inheritance
Comments
Post a Comment