CMPS 280 Flashcards
What is inheritance?
States and behaviors can be passed down.
Give an example of inheritance.
There can be a superclass called Shape. You can create a square, circle, and triangle class that inherits from shape. So now square, circle, and triangle can use all of the methods and attributes that the shape class uses.
In inheritance, child classes can reference the parent class, but…
parent classes can’t reference the child classes.
How do you perform inheritance?
Use the extends keyword. public class Shape{ } public class Square extends Shape{ } public class Triangle extends Shape{ } public class Circle extends Shape{ }
What is an interface?
An interface provides a contract for functionality. If another class inherits from an interface, that class is forced to use all of the methods that are placed inside of an interface.
What is polymorphism?
Polymorphism is the capability of a method to do different things based on the object that it is acting upon.
Why do we use polymorphism?
We use polymorphism if we have classes that would use the same methods but with different actions
Give an example of polymorphism using method overloading.
public class Dog extends Animal
{
public void makeSound() {
System.out.println(“woof!”);
}
public void makeSound(boolean isInjured)
{
System.out.println(“whimper”);
}
}
Give an example of polymorphism using method overriding
public class Animal
{
//properties
public virtual void makeSound() {
}
}
public class Dog : Animal {
//properties
public void override makeSound() {
Console.WriteLine(“woof”);
}
}
What is an accessor and mutator?
An accessor is a fancy word for a getter method. An accessor returns a class’s variable or its value. A mutator is a fancy word for setter. A mutator sets a class’s variable or its value.
Give an example of an accessor and mutator.
public String getName() {
return mName;
}
public void setName( String name ) {
mName = name;
}
What is the “this” keyword?
this is an alias or a name for the current instance inside the instance.
What is an abstract class?
An abstract class has no objects, and can contain abstract methods (but doesn’t have to contain abstract methods)
Shape s = new Circle();
What is an abstract method?
-