Data, Classes, Inheritance
Notes
See weekly notes here
Hacks
Details of a Class
Access Modifier
- Keywords that can be used to control the visibility of fields, methods, and constructors in a class. Examples include:
-
public
- We can access the modifier anywhere, this includes inside and outside the class as well as inside and outside the package. -
protected
- We can access the modifier within the same package and also from outside the package with the help of a child class. If we do not make a child class, then the modifier cannot be accessed outside the package. -
default
- We can only access the modifier within the same package. If we do not specify any access modifier, it will automatically have a default modifier. -
private
- We can access the modifier only within the same class.
-
See more information here
Constructor
- A special method that is used to initialize objects
- The constructor is called when the an object of a class is created
See more information here
Setters & Getters
- Get methods return the variable value
- Set methods sets the value
- Both are used to protect your data, particularly when creating classes
See more information here
Code Challenges
- See code here
Code
Linked List, Queues, and Stacks
/**
* Person.java
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Person.java
*/
public class Person
{
protected String _username;
protected String _name;
protected String _schoolName;
protected String _email;
/**
* Constructor for class
*
* @param name
* @param schoolName
*/
public Person(String name, String username, String schoolName, String email)
{
_name = name;
_username = username;
_schoolName = schoolName;
_email = email;
}
/**
* getName
*
* @return _name
*/
public String getName()
{
return _name;
}
/**
* getYearOfBirth
*
* @return _yearOfBirth
*/
public String schoolName()
{
return _schoolName;
}
/**
* getUsername
*
* @return _username
*/
public String username()
{
return _username;
}
/**
* getYearOfBirth
*
* @return _yearOfBirth
*/
public String email()
{
return _email;
}
/**
* toString
*
* @return description
*/
public String toString()
{
String description =
"Name: " + _name +
"\nUsername: " + _username +
"\nEmail: " + _email +
"\nSchool: " + _schoolName +
"\n";
return description;
}
}
/**
* Student.java
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Student.java
*
*/
public class Student extends Person
{
private String _grade;
/**
* Constructor for class
*
*/
public Student(String name, String username, String schoolName, String email, String grade)
{
super(name, username, schoolName, email);
// TODO Auto-generated constructor stub
_grade = grade;
}
@Override
public String toString()
{
String description =
"Name: " + _name +
"\nUsername: " + _username +
"\nEmail: " + _email +
"\nSchool: " + _schoolName +
"\nGrade: " + _grade +
"\n";
return description;
}
}
/**
* Teacher.java
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Teacher.java
*
*/
public class Teacher extends Person
{
private String _className;
/**
* Constructor for class
*/
public Teacher(String name, String username, String schoolName, String email, String className)
{
super(name, username, schoolName, email);
// TODO Auto-generated constructor stub
_className = className;
}
@Override
public String toString()
{
String description =
"Name: " + _name +
"\nUsername: " + _username +
"\nEmail: " + _email +
"\nSchool: " + _schoolName +
"\nClass Name: " + _className +
"\n";
return description;
}
}
/**
* Main.java
*
* @author Natalie Beckwith
* @version 1
*/
/**
* Main class for Main.java
*
*/
public class Main
{
/**
* main
*
* @param Args
*/
public static void main(String[] Args)
{
Person person = new Person("Person 1", "testUsername123", "Test High School", "email@gmail.com");
System.out.printf("%s\n", person.toString());
Student studnet = new Student("Natalie", "nbeckwith17", "Del Norte High School", "natalieb60251@stu.powayusd.com", "12");
System.out.printf("%s\n", studnet.toString());
Teacher teacher = new Teacher("Teacher", "coding101", "Del Norte High School", "teacher@gmail.com", "Computer Science A");
System.out.printf("%s\n", teacher.toString());
}
}
Main.main(null);