-
Notifications
You must be signed in to change notification settings - Fork 2
Java Classes Objects
Classes are groups of variables and operations on them. A class can have variables, methods (or functions) and constructors (or methods which are used to initiate, more on that later!).
Think of a Class
as a blueprint for creating something concrete. A Class
tells you the 'what' and 'how' an object
of that Class will look like once instantiated
. In essence, it defines properties
(say color, engine capacity) and behavior
(stop, speed up, change gears, honk etc.) for a Car in this case.
Objects are instances of a class. All objects are instances of a certain class. Imagine a class being a "template", which every Object copies to. When you create an Object, basically it creates a new object on the blueprint of a class. Now lets look at this from a little piece of code :
// Car class
public class Car {
// car name
private String name;
// car mannufacturer name
private String manufacturerName;
// constructor
public Car(String name, String man) {
this.name = name;
this.manufacturerName = man;
}
// getter method
public String getName() {
return name;
}
// getter method
public String getManufacturerName() {
return manufacturerName;
}
//setter method
public void setName(String name){
this.name = name;
}
}
Car modelS = new Car("Model S","Tesla");
System.out.println("Full Car Name = " + modelS.getManufacturerName() + " " + modelS.getName());
// prints Tesla Model S
🚀 Run Code
So, Car
is a class, which has the fields or properties name and manufacturerName. modelS
is an object of Car
class. So modelS
also has the same properties and methods.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links