This post shows what is class in Java with an example.
Learn more about classes in Java at https://www.javaguides.net/2018/11/what-is-class-in-java-with-programming-examples.html.
What is a class?
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.
Let’s look at an example of a class and analyze its various parts in a below diagram. This example declares the class Circle, which has the member-variables x, y, and radius of type Integer and the two member-methods, area() and fillColor().
A class is a template for creating objects
Below diagram shows a Circle class which is a template to create three objects:
Java class example
Let's demonstrate how to create a Class in Java with an example.
Here is a Student class:
package net.javaguides.corejava.oops;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Student {
private String name = "Ramesh";
private String college = "ABC";
public Student() {
super();
}
public Student(String name, String college) {
super();
this.name = name;
this.college = college;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
}
Related Java OOPS Examples
- Java Abstraction Example
- Java Inheritance Example
- Java Encapsulation Example
- Java Simple Inheritance Example
- Java Composition Example
- Java Aggregation Example
- Java Delegation Example
- Java Method Overloading Example
- Java Method Overriding Example
- Java Single Inheritance Example
- Java Multilevel Inheritance Example
- Java Hierarchical Inheritance Example
- Java Abstract Class Example
- Java Class Example
Comments
Post a Comment