i am pretty new in object oriented known as java.so i would like ask how can i create an object??am i rite if i say that we have to create an object first before create the class??anyone please help me out
object oriented programming
Started by farhanyun91, Jul 28 2010 09:04 PM
6 replies to this topic
#1
Posted 28 July 2010 - 09:04 PM
|
|
|
#2
Posted 02 August 2010 - 11:52 AM
to create an Object, you should create a new class
public class school{
//if you want to put some attribute, put it here
//lik the following
//int noOfClasses;
//int teachers;
public school(){
//that's called adefault constructor
//in java we prefer to Intialize the variables to zero
// noOfClasses=0;
//teachers=0;
}
main(){
school s=new school(); //now you create Object s of type class
}
}
I hape that's clear.
If you have a nother question write it.
public class school{
//if you want to put some attribute, put it here
//lik the following
//int noOfClasses;
//int teachers;
public school(){
//that's called adefault constructor
//in java we prefer to Intialize the variables to zero
// noOfClasses=0;
//teachers=0;
}
main(){
school s=new school(); //now you create Object s of type class
}
}
I hape that's clear.
If you have a nother question write it.
#3
Posted 04 August 2010 - 01:58 AM
One of the best practices of Object Oriented Programming is to make ur fields private, this helps to encapsulate the data and prevent it to being corrupted.
#4
Posted 05 August 2010 - 07:24 AM
please, can anyone explain this problem
"One of the best practices of Object Oriented Programming is to make ur fields private, this helps to encapsulate the data and prevent it to being corrupted."
thanks,
"One of the best practices of Object Oriented Programming is to make ur fields private, this helps to encapsulate the data and prevent it to being corrupted."
thanks,
#5
Posted 05 August 2010 - 07:44 AM
It means that every attribute from a class should be private or at least protected and never public. If you need it public because another object must be able to change it / read, you should use setter / getter methods.
(If you don't write private or anything like you did in your example it's private by default)
(If you don't write private or anything like you did in your example it's private by default)
#6
Posted 05 August 2010 - 07:48 AM
It simply means, lets say u have a class car
public class Car {
private String model;
private String name;
}
The private privates fields model and name are encapsulated within a class Car, hence the data in those fields cannot be easily changed by anyone who will extent the class, Inside the client of the class can access the fields through the setter and getter methods. which u can control how data is changed.If this is not clear, please say so, i'l elaborate more!
public class Car {
private String model;
private String name;
}
The private privates fields model and name are encapsulated within a class Car, hence the data in those fields cannot be easily changed by anyone who will extent the class, Inside the client of the class can access the fields through the setter and getter methods. which u can control how data is changed.If this is not clear, please say so, i'l elaborate more!
#7
Posted 11 August 2010 - 08:28 AM
@Farhanyun:
I've posted a question about JButtons today, so I'll use that as an example. JButtons are defind as a class somewhere in the Java.Swing library, so they're already availible for use. The JButton class is a template for how all JButtons work, but it is not an object itself. You can create an instance (object) from the class 'template' like this:
JButton button1 - tells Java that you're reserving memory space for a JButton named Button1
button1 = new JButton(); - tells Java that you want Button1 to be created as a basic instance of JButton, with no extra data in the fields. The JButton(); refers to the constructor method of the JButton class, and as there are no parameters provided, it creates a button object with default values, and depending what constructors are availible, you can create new objects that hold some data from the get-go (Don't worry too much about this - you'll learn overloading later :) ).
Hope that helps.
@eman ahmed
They're basically saying that data inside your classesshould be protected from outside interferance. It was explained to me like this:
If you make a piece of software and put it out over the internet, someone else might want to add something to your code, which means that their class objects might need to interact with your class objects. If you make all of the variables public, they could change one without thinking about its impact on the rest of your code (E.g. they could do exampleClass.variable1 = 10, when variable1 was only meant to go up to 9 otherwise there would be an error somewhere down the line), and then they'll complain that your code didn't work. To stop this, make your variables private, but leave public methods in there to manipulate the data. You can write these methods to ensure that anything changed doesn't break the piece of software. (E.g. leave a method called setVariable1(int) to handle the example)
Hope that helps, and isn't just crazy rambling :)
~Fae
I've posted a question about JButtons today, so I'll use that as an example. JButtons are defind as a class somewhere in the Java.Swing library, so they're already availible for use. The JButton class is a template for how all JButtons work, but it is not an object itself. You can create an instance (object) from the class 'template' like this:
JButton button1 = new JButton();
JButton button1 - tells Java that you're reserving memory space for a JButton named Button1
button1 = new JButton(); - tells Java that you want Button1 to be created as a basic instance of JButton, with no extra data in the fields. The JButton(); refers to the constructor method of the JButton class, and as there are no parameters provided, it creates a button object with default values, and depending what constructors are availible, you can create new objects that hold some data from the get-go (Don't worry too much about this - you'll learn overloading later :) ).
Hope that helps.
@eman ahmed
They're basically saying that data inside your classesshould be protected from outside interferance. It was explained to me like this:
If you make a piece of software and put it out over the internet, someone else might want to add something to your code, which means that their class objects might need to interact with your class objects. If you make all of the variables public, they could change one without thinking about its impact on the rest of your code (E.g. they could do exampleClass.variable1 = 10, when variable1 was only meant to go up to 9 otherwise there would be an error somewhere down the line), and then they'll complain that your code didn't work. To stop this, make your variables private, but leave public methods in there to manipulate the data. You can write these methods to ensure that anything changed doesn't break the piece of software. (E.g. leave a method called setVariable1(int) to handle the example)
Hope that helps, and isn't just crazy rambling :)
~Fae


Sign In
Create Account


Back to top









