Special Java keyword that refers to the current instance that is executing the method
In the example below, this is used to distinguish name (the instance variable), and name( the method parameter)
In this class, we will ask you to avoid using the same parameter names and instance variable names (checkstyle is your friend)
You will mostly see this used to call constructors in constuctor overloading
publicclassDog{
private String name;
Dog(String name) {
this.name = name; // checkstyle WILL give you an error if you try this
}
}
Practice with This Keyword (10 minutes)
Create a class called Country. Set up three instance variables inside of Country: name, nationalBird, and populationSize. Create a constructor for Country that uses the this keyword to set up all three instance variables.
Create a second constructor for Country that takes in a nationalBird and name, but not populationSize. Use the this keyword to call the first constructor and set the value of populationSize to 100.
Static Methods and Static Fields
Static Methods
Use the static keyword to attach the method to the class, rather than the instance
static class methods cannot access instance variables
Static Fields
Typically used to establish constants and make code more readable
Commonly Used with the final keyword to create symbolic constants
final creates a variable that cannot be changed after it has been set
Static Method - Example
publicclassBuilding{
private String name;
privateint height;
publicBuilding(String name, int height){
this.name = name;
this.height = height;
}
publicstatic String getName(){
return name;
}
}
Building siebel = new Building("Siebel", 4);
// Will this work? why/why not?
System.out.println(siebel.getName());
Practice with Static Methods and Fields (10 minutes)
Create a class named Dog. Dog should have name and age instance variables. Your Dog class should come with a constructor that sets the name and age. Use the this keyword when appropriate.
Create a method called methodA that returns a String that says "This is the Dog class."
Create a method called methodB that returns a String that says "This dog's name is { }". The dog's name should appear where the curling braces are.
Between methodA and methodB, which one can be made into a static method? Why or why not? Use the static keyword to make it a static method.
Inheritance
Inheritance allows us to create relationships between classes. We can inherit the behavior of one class by extending another class. Inheritance can be useful when we have objects that share functionality.
extends - keyword used to create a one way relationship between two classes; the child class extends the parent class super - keyword used by the child constructor to call the parent constructor; MUST be called at the start of the constructor
Private variables in the parent class can't be accessed by the child classes, but child classes can access protected variables in the parent class.
Inheritance
What gets printed here?
classBase{
publicvoidshow(){
System.out.println("Base::show() called");
}
}
classDerivedextendsBase{ // here the child class is extending the parent class publicvoidshow(){
System.out.println("Derived::show() called");
}
}
Base b = new Derived();
b.show();
Inheritance
publicclassAnimal{
protected String name;
protected String sound;
publicAnimal(String n, String s){
name = n;
sound = s;
}
}
publicclassCatextendsAnimal{
publicdouble cutenessLevel;
protectedboolean hasClaws;
publicCat(String n, String s, double c, boolean h){
super(n, s); // this is how we use the super keyword to call the parent class's constructor
cutenessLevel = c;
hasClaws = h;
}
}
Practice with Inheritance (Fish) (5 minutes)
Create a class called Animal. Animal has one method called sleep() that returns "I am sleeping."
Create a class called Fish. Fish is a child class of Animal. Fish has one method called swim. Fish should print "I am swimming." inside of swim(). Use extends correctly so that a Fish object can sleep and swim.
Practice with Inhertiance (Computer) (10 minutes)
Create a class called Computer. The Computer class has two instance variables: name and batteryLife. Create a constructor for Computer that initilizes these two variables.
Create a class called DellComputer. DellComputer should inherit the same instance variables as the Computer class, but it also has an instance variable called screenSize of integer type. Create a constructor for DellComputer that uses the super keyword and initilizes screenSize.
Create a class called AppleComputer. AppleComputer should inherit the same instance variables as the Computer class, but it also has an instance variable called ios of String type. Create a constructor for AppleComputer that uses the super keyword and initilizes ios.
Identify the parent class and the child class(es) in this scenario.
That's All, Folks!
Thanks for stopping by. Leave us some feedback pls.
See y'all on Friday!
Acknowledgements: Special thanks to course staff from Fall 2020 for providing some of the coding examples seen in this slide deck.
Practice with Static Methods and Static Fields - Solution
publicclassDog{
private String name;
privateint age;
publicDog(String setName, int setAge){
this.name = setName;
this.age = setAge;
}
// we can make this method static because it is not dependent on the instance publicstatic String methodA(){
return"This is the Dog class.";
}
public String methodB(){
return"This dog's name is " + name;
}
}
Dog happy = new Dog("Happy", 10);
System.out.println(happy.methodB());
System.out.println(happy.methodA());
Practice with Inheritance (Fish)
publicclassAnimal{
public String sleep(){
return"I am sleeping";
}
}
publicclassFishextendsAnimal{
public String swim(){
return"I am swimming";
}
}
Fish nemo = new Fish();
System.out.println(nemo.swim());
System.out.println(nemo.sleep());
Practice with Inheritance (Computer)
// Computer is the parent class publicclassComputer{
String name;
int batteryLife;
publicComputer(String setName, int setBatteryLife){
this.name = setName;
this.batteryLife = setBatteryLife;
}
}
// DellComputer is a child class of Computer publicclassDellComputerextendsComputer{
int screenSize;
publicDellComputer(int setScreenSize){
super("Dell Computer", 12);
this.screenSize = setScreenSize;
}
}
// AppleComputer is a child class of Computer publicclassAppleComputerextendsComputer{
String ios;
publicAppleComputer(String setIos){
super("Apple Computer", 12);
this.ios = setIos;
}
}