D4 - Discussion Forum Unit4 PDF

Title D4 - Discussion Forum Unit4
Author Majd Haddad
Course Programming 1
Institution University of the People
Pages 6
File Size 105.4 KB
File Type PDF
Total Downloads 85
Total Views 144

Summary

Discussion Forum Unit4...


Description

The class and the object in Java Class concept in Java Class is a large container that can contain all the code from variables, functions, objects, etc. To define a new class, just write a class, give it a name, and open parentheses specifying its beginning and end

Concept of Attributes Any variables that are defined inside the class and outside any function called Attributes, and this means that any object of this class will have these properties. You can manipulate these properties directly from the object, while normal variables cannot be manipulated from the object. Variables that are placed parametres or that are defined within functions are called regular variables.

Object concept in Java Object: means object in the Arabic language. An object is an exact copy of a specific class. Since an object is a clone of the class, we can say that an object cannot be created if there is no class. So, in the concept of object programming, we create a specific class that they call blue print (i.e., the raw version or the original version), and then we create one or more copies of this class and do with it what we want without changing the contents of the basic class and thus we have preserved the basic class codes because we are We modify copies, not directly.

Since the object is a clone of the class. To define an object of a particular class, you must put the class name and then put a name for the object.

How to deal with objects • We create an object from the class. • Then we enter values for its properties, call its functions, etc.

Calls anything contained in the object we created 1 - We put the name of the object. 2 - Then the point. 3 - Then the thing that we want to access (whether it is the name of a variable or a function).

Object-to-class relationship in Java Objects help the programmer a lot, for example if you intend to create a simple program to save people's information, will you create a class for each person ?! Of course not, but you create only one class that represents a person, and put in it the basic things that you want to be present in each person. Then you create as many creatures from it as you want, and then each object of this class becomes a person with his own information.

Example : We have created a class that contains the basic information we want to fill out for each person. Then we created 4 objects (4 people), then inserted private information for each object in them. Now if you add any new variable or function in the Person class, any object in that class will have a copy of the new thing that you added. And if you modify a specific code in the Person class, this code will also be modified for all objects of this class.

Puplic class Person { // Here we define 4 Attributes String name; String sex; String job; int age; // Here we define a function that prints the content of each property when it is called void printInfo()

{ System.out.println("Name: " +name); System.out.println("Sex: " +sex); System.out.println("Job: " +job); System.out.println("Age: " +age); } } Public class Main { public static void main(String[] args) { // Here we have created objects from the class Person Person p1 = new Person();

// p1 will represent the object jad

Person p2 = new Person();

// p2 will represent the object mia

Person p3 = new Person();

// p2 will represent the object rone

Person p4 = new Person();

// p2 will represent the object sami

// Here we have defined the properties of the object p1 p1.name = "Jad"; p1.sex = "Male"; p1.job = "Teacher"; p1.age = 33;

// Here we have defined the properties of the object p2 p2.name = "Mia"; p2.sex = "Female"; p2.job = "Dotor"; p2.age = 29;

// Here we have defined the properties of the object p3 p3.name = "Rone"; p3.sex = "Male"; p3.job = "Engineer"; p3.age = 30;

// Here we have defined the properties of the object p4 p4.name = "Sami"; p4.sex = "Male"; p4.job = "Lawyer"; p4.age = 25;

// Here we have shown the properties of each object p1.printInfo(); p2.printInfo(); p3.printInfo(); p4.printInfo();

} }...


Similar Free PDFs