#basic classes in java
1 messages · Page 1 of 1 (latest)
god i hate courses that frontload inheritance like this
well okay the basic rule is that to make an instance of a class, you need to call its constructor
public class Person {
private String name;
private int age;
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
public Person(String name, int age){
this.name = name;
this.age = age;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
public String getName(){
return name;
}
public int getage(){
return age;
}
}
thats this code
and the reason is that this "constructor" can do stuff like
"hey, people can't be negative years old"
public class Person {
private String name;
private int age;
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
public Person(String name, int age){
if (age < 0) {
// CRASH
throw new IllegalArgumentException();
}
this.name = name;
this.age = age;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
public String getName(){
return name;
}
public int getage(){
return age;
}
}
so if you "extend" a person, you still need to run its constructors
thats what super(...) is doing
public class Student extends Person {
private ArrayList<String> courses;
public Student(String name, int age) {
super(name, age);
courses = new ArrayList<>();
}
}
its saying "okay run the code that initializes that part of me that is a student"
now if you just learned what classes are
and then immediately learned what extending classes was
uhh
lets just say its not really surprising you are lost
why are parameter needed for the public Student method?
well, because it was declared that way
because person is part of student
all that you really need to do is make sure to provide a name and age to a person
public class Bob extends Person {
public Bob() {
super("bob", 24);
}
}
so here, the public Bob method, as you call it
doesn't need parameters
it just decided on values
imagine it like student has a person object inside
and inside that person are 2 wolves
so Student needs a name and age because it is declared to want a name and age
but whether it gets them from the outside or decides on values itself, to make a Person you need a name and age
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
public Person(String name, int age){
this.name = name;
this.age = age;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
because this is the only constructor for Person
and it must be called
public class Joe extends Person {
public Joe(int age) {
super("joe", age);
}
}
another example
ok i get it
public class Student extends Person {
private courses;
ArrayList<String> courses = new ArrayList<String>();
public Student(String name, int age) {
super(name, age);
}
also why cant i write the code like this?
im kinda confused when to use a format like this
ArrayList<String> courses = new ArrayList<String>();
and when to do it the other way
Detected code, here are some useful tools:
well okay so
there are two "kinds" of variables
fields and just regular variables
all variables need to have a type declared
so just private courses; won't work
basically inside a method you can do
String name = "bob";
and that is valid for a field too
so
class Example {
String name = "bob";
}
and
class Example {
void doThing() {
String name = "bob";
System.out.println(name);
}
}
both work
now, they do different things
inside the void doThing() its just a "local variable"
and fields can be seen by things outside the class
in both cases you can split up the declaration of the field/variable and the assignment of the initial value
class Example {
void doThing() {
String name;
name = "bob";
System.out.println(name);
}
}
but with a field, you can't just put an assignment on its own line
that code needs to be in a constructor
class Example {
String name;
// name = "bob"; won't work here
Example() {
// has to go in a constructor
name = "bob";
}
}
then, seperately, fields can have "visibility modifiers"
by default, everything can be "seen" by code in the same folder
like
// ...
Example e = new Example();
System.out.println(e.name);
so by "see" I mean you can write e.name
if you make something private then only code in the same file can see it
class Example {
private String name;
// name = "bob"; won't work here
Example() {
// has to go in a constructor
name = "bob";
}
}
so e.name will work in this file, but not in other ones
if its public it can be seen across every file in your program
class Example {
public String name;
// name = "bob"; won't work here
Example() {
// has to go in a constructor
name = "bob";
}
}
now you might be thinking: hey wait a second
i am writing everything in one folder and will be for awhile
why do i have to write public on everything?
good question strawman: your teacher and the curriculum they are teaching off of is exhausting
Book teaching how to write modern and effective Java.
Book teaching how to write modern and effective Java.
import java.util.ArrayList;
public class Student extends Person {
private ArrayList<String> courses;
public Student(String name, int age) {
super(name, age);
courses = new ArrayList<>();
}
import java.util.ArrayList;
public class Student extends Person {
private ArrayList<String> courses;
public Student(String name, int age) {
super(name, age);
ArrayList<> courses = new ArrayList<>();
}
last question, why does the second way not work? it looks like the same to me
Detected code, here are some useful tools:
both will have empty arraylist
nope
the second one the empty array list will be "local" to the constructor
and the field courses will be null, not an empty array list
class Dude {
String name;
Number() {
// This "name" and the field name are different
String name = "bob";
// the field is never initialized so it is `null`
}
}
class Dude {
String name;
Number() {
// This "name" unambiguously refers to the field
name = "bob";
// the field is initialized to "bob"
}
}
class Dude {
String name;
Number() {
// This "name" is different than the field
String name = "bob";
// So any changes to it will not change the field
name = "susan"; // Java assumes you mean the local variable
}
}
class Dude {
String name;
Number() {
// This "name" is different than the field
String name = "bob";
// So any changes to it will not change the field
name = "susan"; // Java assumes you mean the local variable
// but if you put "this." that disambuguates it - makes it not ambiguous
// and it will actually set the field
this.name = "joe";
}
}
okay ty
Closed the thread.
don't delete messages when you are done
just close the thread