Chapter 3 Flashcards
(36 cards)
Create a class called car that has attributes and methods
class car { String color;
void startEngine() { // Some code } }
Create a class that inherits from base class
class JamesBondCar extends car { boolean hasGuns = false;
void startSwimming() { //Some code here } }
Create an instance of a class
Instance of a class is a variable - var type is that of the class using the “new” keyword
JamesBondCar meq_car = new JamesBondCar();
Heap Memory
Part of memory where instances of classes (objects) are stored
FINAL Variables, methods, class
Variables that will never change
final String myName = “Ejaz”;
Methods declared as final CAN NEVER be overwritten
static final int sumOfTwoNums(int a, int b)
If a class is Final, no one can subclass it Ex: String class is immutable
Primitive Data Types in Java
byte = 8 bits short = 16 bits int = 32 bits long = 64 bits float = 32 bits double = 64 bits char = 16 bits boolean = true/false
Wrapper Classes
All primitive data types have corresponding classes called wrapper classes that contain useful methods.
Some Java collections can't store primitives and hence primitives are wrapped into objects ArrayList meq_al = new ArrayList(); meq_al.add(6); meq_al.get(2) -> returns 3rd element
What does the method
public double calcTax()
in a class imply
- Any external class can access this method (as it is public)
- Returns a double value
What does the keyword “private” mean
Member var or method is only accessible within the class Most restrictive Ex: A class can have methods that are just needed by the class. Users of the class have nothing to do with it. Such methods should be private
What is the default access level
package
Classes in the package will have access to this method
How do you decide access level
Experience:::
TIP: Make everything private. Hand out higher level access as needed
What should a class encapsulate
BEHAVIOR it supports
How do you implement an interface
A class may need behavior that is not directly related to it. Such behavior can do in an interface and the class can “implement” this interface
Ex: class Employee implements Payable { // Some code goes here }
When a class declares that it implements an interface, it guarantees to provide implementation for all methods in that in the interface.
Think of the interface as a HEADER file
Example of interface
PAYABLE INTERFACE: public interface Payable { boolean increasePay(int percent); } CLASS EMPLOYEE class Employee implements Payable { boolean increasePay(int percent) { } } CLASS CONTRACTOR class Contractor(int percent) { boolean increasePay(int percent) { } }
Both Contractor and Employee contain their own versions of increasePay()
Data Type of vars in interface
All variables declared in an interface automatically become
public static final
Default Methods in an Interface
Interface may define a method with a default behavior using the “default” keyword
public interface Payable { default boolean increasePay(int percent) { System.out.println("Hiya"); return true; } }
Hence if a class does not have its own implementation of this method, the default will be used
What is the Class Object
The class Object sits on top of the class hierarchy. All Java Classes form an Inheritance tree with Object on top of the hierarchy. All Java Classes are direct or indirect descendants of Object
Which of the following is legit for the following class decleration
class NJTax extends Tax { }
NJTax myTax1 = new NJTax(); Tax myTax2 = new NJTax(); Object myTax3 = new NJTax();
All
Tax myTax2 = new NJTax(); Object myTax3 = new NJTax();
are said to be upcasting
Inheritance
Ability to define a new class using an existing one. Special keyword extends is used to indicate that one class is inherited from another Ex: class ejaz extends human { }
Method overriding
Ability to define some different functionality for a method than the functionality used in the superclass
Where do you use method overriding
- Source code of superclass is not available
- Original version is still valid and needs to be kept intact
- To enable polymorphism
When creating a class
Create 2 constructors 1. Default with no parameters public car() { } 2. Another one with parameters public car(String color, int numCylinders) { this.color = color; this.numCylinders = numCylinders; }
When inheriting from a super class -
Define the super constructor without paramters public class meqCar extends car { public meqCar() { super(); } }
Characteristics of constructors
- Called when class is instantiated
- Must have the same name as the class they are in
- Can’t return a value and don’t specify void as return type