Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to represent real-world entities and their interactions.
The goal of OO Programming is to keep our code organized.
It aims to make code more organized, modular, and easier to maintain. There are four main principles of OOP:
Encapsulation: The bundling of data and methods that operate on that data within a single unit (class).
Inheritance: The ability for a class to inherit properties and methods from a parent class.
Polymorphism: The ability for a method to have different implementations based on the object it is acting upon.
Abstraction: The process of simplifying complex systems by breaking them down into smaller, more manageable parts.
There are 4 kinds of Data Members that can be in a Java Class:
Class Static Variables
Class Instance Variables
Method Local Variables
Loop Variables.
Note: The this keyword is a reference to an OBJECT. NOT a class.
There are 4 kinds of visibility modifiers in Java:
Public
Private
Protected
default
When we use the new keyword: We are making an OBJECT from a CLASS.
Student joe = new Student();
2 methods are overloaded if they have the same method name but they differ in the method signature: that is, the parameters we are passing in to the Method Declaration.
For your Test Questions: Keep track of the difference between defining a method and calling a method.
public class person{
static String name;
static int age;
static String program;
public static void main(String[] args) {
RunProgram();
}
public static void RunProgram(){
Introduce();
}
public static void Introduce() {
System.out.print("HELLO My name is " + name +
" My age is " + age +
" My program is " + program
);
}
}
To make the Introduce() method non-static, remove the static keyword.
Then, to call it from the RunProgram() method,
you need to create an instance of the Person class.
public class Person {
String name;
int age;
String program;
public static void main(String[] args) {
RunProgram();
}
public static void RunProgram() {
Person personInstance = new Person();
personInstance.Introduce();
}
public void Introduce() {
System.out.print("HELLO My name is " + name +
" My age is " + age +
" My program is " + program
);
}
}
Note that I also changed the class name to Person with an uppercase 'P' to
follow Java naming conventions for class names.
Exercise 1: Creating a Class
Task: Create a simple Java class named "Person" with no attributes or methods.
publicclassPerson {
}
Exercise 2: Adding Attributes
Task: Add three attributes to the "Person" class: name, age, and gender.
java
Copy code
publicclassPerson {
String name;
int age;
String gender;
}
Exercise 3: Creating Methods
Task: Create a method called introduce() in the "Person" class that prints the person's name, age, and gender.
publicclassPerson {
String name;
int age;
String gender;
publicvoidintroduce() {
System.out.println("Hi, my name is " + name + ", I am " + age + " years old, and I am a " + gender + ".");
}
}
Exercise 4: Creating an Object
Task: Create an object of the "Person" class and assign values to its attributes. Call the introduce() method for the object.
java
Copy code
publicclassMain {
publicstaticvoidmain(String[] args) {
Person person1 = newPerson();
person1.name = "Alice";
person1.age = 30;
person1.gender = "female";
person1.introduce();
}
}
Exercise 5: Using Getters and Setters
Task: Add getters and setters for the attributes of the "Person" class.
java
Copy code
publicclassPerson {
privateString name;
privateint age;
privateString gender;
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicintgetAge() {
return age;
}
publicvoidsetAge(int age) {
this.age = age;
}
publicStringgetGender() {
return gender;
}
publicvoidsetGender(String gender) {
this.gender = gender;
}
publicvoidintroduce() {
System.out.println("Hi, my name is " + name + ", I am " + age + " years old, and I am a " + gender + ".");
}
}
Exercise 6: Constructors
Task: Add a constructor to the "Person" class that accepts name, age, and gender as parameters.
java
Copy code
publicclassPerson {
privateString name;
privateint age;
privateString gender;
publicPerson(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (