Class & Object in java - Do you know ?????

To connect more and discuss technology, Please visit and like the page. We will list all the posts there and provide updates about upcoming events/posts.

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.


                      What are Concept of oops in JAVA ?  


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();
}

}


Run the Above Class--

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 - 

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages.This principle can also be applied to object-oriented programming and languages like the Java.In java polymorphism is two type -

1. Compile time polymorphism
2. Run time polymorphism

                poly2


Upcoming Posts - 

We will have more fun and learning in upcoming weeks. Bookmark this space in your favourites. 
1. Polymorphism
2. Inheritance
 

You can also suggest your favourite topics in comments section or on Facebook page. We will try to start exploring on those topic. You can also suggest points to make blogs more effective.

To connect more and discuss technology, Please visit and like the page. We will list all the posts there and provide updates about upcoming events/posts.

Comments

Popular posts from this blog

Inheritance - Object-oriented programming (Parent-Child)

Polymorphism Part 1 (Compile Time Polymorphism)