Module 2: Classes 2 Flashcards
What is encapsulation?
The process of hiding data from users by making attributes of a class only accessible through methods of the same class. To encapsulate, create private attributes and provide public get and set methods.
Ex:
class BankInfo {private double balance = 0; public void deposit(double x) {if(x>0) {balance += x;}}}
What is inheritance?
The process of a class acquiring attributes and methods from another class. The class giving is the superclass and the class receiving is the subclass. To inherit, use the extends keyword.
Ex:
class Mclaren extends Car {}
The subclass inherits all non private attributes and methods, and constructors are not inherited. However the constructor of the superclass is called when the subclass creates new objects.
You can access members of the superclass through the subclass by using the “super” keyword.
What is polymorphism?
The idea that something can have many forms. To achieve polymorphism, have a class inherit from another then create an object of the subclass.
Ex:
class Animal {public void noise() {System.out.println(“Grr”);}}
class Cat extends Animal {public void noise() {System.out.println(“Meow”);}}
class Dog extends Animal {public void noise() {System.out.println(“Woof”);}}
public static void main(String[] args) {Animal a = new Cat(); Animal b = new Dog(); a.noise();}
What is method overriding?
When a subclass implements a superclasses method with the same name but performs a different action. Method overriding is also known as “runtime polymorphism”. When you override a method, you should use the @Override annotation.
Ex:
class Animal {public void noise() {System.out.println(“Grr”);}}
class Cat extends Animal {@Override public void noise() {System.out.println(“Meow”);}}
class Test {public static void main(String[] args) {Cat c = new Cat(); c.noise();}}
Overriding rules:
- Should have the same return type and arguments
- The access level cannot be more restrictive than the method being overridden (if the superclass method is public the subclass overriding method cannot be private or protected)
- A method static or final cannot be overridden
- If the method can’t be inherited, it cannot be overridden
- Constructors cannot be overridden
What is method overloading?
When you have a defined method but want to to perform 2 or more different tasks. This is also known as “compile time polymorphism”. Methods can have the same name as long as their number and/or type of parameters are different.
Ex:
class A {public void doSomething() {System.out.println(“A”);}
public void doSomething(String str);}}
class B {public static void main(String[] args) {A object = new A();
object.doSomething(“B”);}}
doSomething is being overloaded from initially being able to only accept no parameters to being able to accept a string parameter.
What is abstraction?
The process of hiding implementation details while only showing essential information to the user. To achieve abstraction, use the abstract keyword. To access abstract classes and methods, it must be inherited from another class and the abstract method body is provided there.
Ex:
abstract class Animal {public abstract void animalSound(); public void sleep() {System.out.println(“zzz”);}}
class Cat extends Animal {public void animalSound() {System.out.println(“Meow”);}}
class Main {public static void main(String[] args) {Cat myCat = new Cat(); myCat.animalSound(); myCat.sleep();}}
An abstract class:
- Is a restricted class and cannot create objects (to access it, it must be inherited from another class)
An abstract method:
- Can only be used in an abstract class, and does not have a body, it is provided by the subclass (abstract methods do not have curly braces)
What is an interface?
A completely abstract class that contains only abstract methods. To create an interface use the “interface” keyword. Classes can implement interfaces using the “implements” keyword.
Ex:
interface Animal {public void eat(); public void makeSound();}
class Cat implements Animal {
public void makeSound() {System.out.println(“Meow”);} public void eat() {System.out.println(“Yummy”);}}
public class Program {public static void main(String[] args) {Cat c = new Cat(); c.eat();}}
You can implement multiple interfaces, just separate them by a comma. When you implement an interface you need to override all of its methods.
Some information about interfaces:
- Cannot contain a constructor because objects cannot be created from an interface
- Interfaces can extend other interfaces
- A class can implement any number of interfaces
- You do not need the abstract keyword, it is already implied (interfaces are default PUBLIC ABSTRACT)
- Interface methods are default PUBLIC ABSTRACT
What is type casting?
The process of changing a variables primitive data type to another. There are 2 types, up cast and down cast. Up cast is automatic and down cast is manual.
Ex up cast:
int myInt = 9;
double myDouble = myInt;
Ex down cast:
double myDouble = 9.78;
int myInt = (int) myDouble;
In down casting, you have to specify the primitive type you want to cast to in parentheses (being int in the example).
What is an inner class?
A nested class, the purpose is to group classes that belong together to make code more readable. Inner classes can access the outer classes attributes and methods.
Ex:
class Outer {int x = 10;
class Inner {int y = 5;}}
To access the inner classes members, you need to create an outer class object, then an inner class object. The exception to this is a static inner class, where you only need to create an inner class object. Static inner classes do not have access to the outer classes members.
Ex:
public class Main {public static void main(String[] args) {Outer a = new Outer(); Outer.Inner b = a.new Inner();
System.out.println(a.y + b.x);
Ex static inner class:
Outer.Inner a = new Outer.Inner();
What is an anonymous class?
An inner class with no name and which only a single object is created. They are useful for making an instance of an object with extras such as overriding methods of a class, without having to subclass.
Ex:
class Machine {
public void start() {System.out.println(“Starting…”);}}
class Program {public static void main(String[] args) {Machine m = new Machine() {@Override public void start() {System.out.println(“Woooo”);}}; m.start();}}
The “@Override” section is the anonymous class.
What is an enum?
A special type that defines a group of unchangeable variables. To create an enum, use the enum keyword and separate the values by commas. All values should be in full caps.
Ex:
enum Rank {
IRON,
GOLD,
DIAMOND}
Enums can also be inside a class, and can be looped through with the values() method in a for loop.
Ex:
public class Main {enum Rank{
IRON,
GOLD,
DIAMOND}
public static void main(String[] args) {
for (Rank a : Rank.values()){System.our.println(a);}}}
Some properties of enums below:
- Enums can have attributes and methods like classes, all enum constants/attributes are public static final (they cannot be changed or overriden).
- Enums cannot create objects, and cannot extend from classes, however they can implement interfaces.