Thusday's CDT section will now start at 5 to match the sceduled course time (banner)
Eastern timezone sessions, and Tuesdays will continue at sceduled times
Make sure to keep up with the calendar for links and timings
3 practice problems every session
focused on practical understanding of concepts covered recently in class
slides and material available after class
Small breakout rooms of working together
Work for 10-15 minutes together, feel free to use paper, whiteboards, online share tools.
Use the jeed playground on the 125 homepage for the interactive running
this is used to distinguish name(the instance variable), and name(the method param)public class Dog {
private String name;
Dog(String name) {
this.name = name;
}
}
static keyword to attach the method to the class, rather than the instancestatic class methods cannot access instance variablespublic class Building {
private String name;
private int height;
public Building(String name, int height) {
this.name = name;
this.height = height;
}
public static String getName() {
return name;
}
}
Building siebel = new Building("Siebel", 4);
// Will this work? why/why not?
System.out.println(siebel.getName());
class SubClass extends BaseClassclass Chuchu extends PetWhat do you think happens here?
public class Animal {
public Animal(){
System.out.println("New animal created!");
}
}
public class Cat extends Animal {
public Cat(){
System.out.println("New cat created!");
}
}
public class Example {
public static void main(String[] args){
Animal c1 = new Cat();
}
}
What gets printed here?
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Private variables in the parent class can't be accessed by child classesprotected variables in the parent classthe super keyword is used in the child constructor to call the parent constructor
it has to be called at the start of the constructor.
public class Animal {
protected String name;
protected String sound;
public Animal(String n, String s){
name = n;
sound = s;
}
}
public class Cat extends Animal {
public double cutenessLevel;
protected boolean hasClaws;
}
superCreate a class named Rectangle containing (doubles) length and width. Then create two methods to print the area and perimeter.
Let class 'Square' inherit Rectangle with its constructor having a parameter for its side (suppose s), and call the constructor of its parent class as 'super(s,s)'.
class Rectangle{
int length;
int breadth;
// don't forget Your constructor!
public void findArea(){
// code here
}
public void findPerimeter() {
//code here
}
}
class Square extends Rectangle{
// code here
// don't forget Your constructor!
}
polymorphism is the provision of a single interface to entities of different types
We can use instanceof to check if an object belongs to a class
Object Cat as an Object AnimalFix the following upcast:
public class Person{
int age;
Person(int setAge) {
age = setAge;
}
}
public class Child extends Person{
int age;
boolean kid;
Child(int setAge, boolean setKid) {
age = setAge;
}
}
Spoilers :P
public class Animal {
protected String name;
protected String sound;
public Animal(String n, String s){
name = n;
sound = s;
}
}
public class Cat extends Animal {
public double cutenessLevel;
protected boolean hasClaws;
public Cat(String n, String s, double c, boolean h) {
super(n, s);
cutenessLevel = c;
hasClaws = h;
}
}
public class Rectangle {
int l;
int b;
public Rectange(int setL, int setB) {
l = setL;
b = setB;
}
public void findArea() {
return l * b;
}
public void findPerimeter(){
return 2 * (l + b);
}
}
public class Square extends Rectangle {
public Square(int setSize) {
super(setSize, setSize);
}
}