🚀 Want to Master Core Java & OOP Concepts?
Subscribe to Ram N Java for beginner-friendly coding tutorials and practical guides!
🔔 Subscribe to YouTube ChannelJava Class Basics: Exploring the Person Class
In Object-Oriented Programming (OOP), modeling human entities is one of the most common requirements across software applications. Designing a Person class in Java demonstrates how real-world human attributes—such as name, age, gender, height, and weight—are bundled with everyday actions into a reusable, well-structured Java blueprint.
1. Real-World Modeling: State and Behavior
Every entity modeled in OOP incorporates state and behavior:
- State (Attributes): The data describing a person, such as
name,age,sex,height, andweight. - Behavior (Actions): The operations or routines associated with a person, such as
eat()andfight(). - Blueprint Definition: The
Personclass defines the structural blueprint, while each individual person created in the program exists as an independent object occupying its own memory.
2. Encapsulation and Data Security
Encapsulation protects an object's internal state from unintended external modification:
- Member Variables: Declare fields inside the class body to represent object characteristics.
- Method Modularity: Implement dedicated methods to execute specific operations rather than hardcoding actions outside the class.
- Reusability: Once declared, multiple person instances (such as Jackie Chan or Angelina Jolie) can be instantiated from the single blueprint.
3. Creating and Managing Person Instances
Instantiating and interacting with individual person objects follows standard Java OOP patterns:
- Instantiation: Use the
newkeyword to allocate heap memory for an instance (e.g.,Person jackieChan = new Person();). - Configuring State: Assign property values directly or through methods.
- Triggering Behaviors: Call methods via reference variables using the dot operator to execute actions tailored to that individual.
4. Best Practices for Designing Entity Classes
When building foundational classes in Java:
- Structure your programs logically by keeping data definitions cohesive and focused.
- Use standard naming conventions: CamelCase for variables/methods and PascalCase for class names.
- Separate model definitions from test/driver logic by running experiments inside a dedicated demo class like
PersonDemo.
📺 Explore More Java Tutorials
Continue expanding your Java and OOP knowledge with these related tutorials from our channel:
- 👉 What is a Class? | Java Class Structure and Syntax Explained | Java Tutorial
- 👉 Java Class Fundamentals: The Dog Class Explained | Java Class | Java Tutorial
- 👉 Java Class Basics: Creating and Using the Bird Class | Java Class | Java Tutorial
Click here to watch on YouTube:
https://www.youtube.com/watch?v=vkDTYu-uOgI&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
Person.java
/** * Person class is the blueprint from which individual Person objects can be * created. */ public class Person { /* * Attributes/Properties/Characteristics. */ String name; int age; String sex; String height; String weight; public void eat(String string) { System.out.println(string); } public void fight(String string) { System.out.println(string); } }
PersonDemo.java
public class PersonDemo { public static void main(String[] args) { Person jackieChan = new Person(); /* * Setting Attributes/Properties/Characteristics. */ jackieChan.name = "Jackie Chan"; jackieChan.age = 53; jackieChan.sex = "Male"; jackieChan.height = "5’.9’'"; jackieChan.weight = "52 Kg"; System.out.println("Attributes"); System.out.println("--------------"); displayPersonInformation(jackieChan); System.out.println("\nBehaviors"); System.out.println("----------"); jackieChan.eat("Can eat only Chinese food"); jackieChan.fight("Can do Martial art or Kung Fu Fight"); System.out.println("*********************************************"); Person angelinaJolie = new Person(); /* * Setting Attributes/Properties/Characteristics. */ angelinaJolie.name = "Angelina Jolie"; angelinaJolie.age = 45; angelinaJolie.sex = "FeMale"; angelinaJolie.height = "6’.2’'"; angelinaJolie.weight = "45 Kg"; System.out.println("Attributes"); System.out.println("--------------"); displayPersonInformation(angelinaJolie); System.out.println("\nBehaviors"); System.out.println("----------"); angelinaJolie.eat("Can eat only American food"); angelinaJolie.fight("Can do karate fight"); System.out.println("*********************************************"); } public static void displayPersonInformation(Person person) { System.out.println("Name : " + person.name); System.out.println("Age : " + person.age); System.out.println("sex : " + person.sex); System.out.println("Height : " + person.height); System.out.println("Weight : " + person.weight); } }
Output
Attributes -------------- Name : Jackie Chan Age : 53 sex : Male Height : 5’.9’' Weight : 52 Kg Behaviors ---------- Can eat only Chinese food Can do Martial art or Kung Fu Fight ********************************************* Attributes -------------- Name : Angelina Jolie Age : 45 sex : FeMale Height : 6’.2’' Weight : 45 Kg Behaviors ---------- Can eat only American food Can do karate fight *********************************************
To Download PersonDemoStateAndMethods Project Click the below link:
https://sites.google.com/site/javaee4321/java/PersonDemoStateAndMethods.zip?attredirects=0&d=1
Nyc Website i love this website please visit my website thanks
ReplyDeleteBiography
Age
Naghma Rizwan Web Series
thanks for visiting