Chapter 3 Flashcards

(36 cards)

1
Q

Create a class called car that has attributes and methods

A
class car
{
 String color;
 void startEngine()
 {
  // Some code
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a class that inherits from base class

A
class JamesBondCar extends car
{
 boolean hasGuns = false;
 void startSwimming()
 {
  //Some code here
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create an instance of a class

A

Instance of a class is a variable - var type is that of the class using the “new” keyword

JamesBondCar meq_car = new JamesBondCar();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Heap Memory

A

Part of memory where instances of classes (objects) are stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

FINAL Variables, methods, class

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Primitive Data Types in Java

A
byte = 8 bits
short = 16 bits
int = 32 bits
long = 64 bits
float = 32 bits
double = 64 bits
char = 16 bits
boolean = true/false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Wrapper Classes

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the method

public double calcTax()

in a class imply

A
  1. Any external class can access this method (as it is public)
  2. Returns a double value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the keyword “private” mean

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the default access level

A

package

Classes in the package will have access to this method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you decide access level

A

Experience:::
TIP: Make everything private. Hand out higher level access as needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What should a class encapsulate

A

BEHAVIOR it supports

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you implement an interface

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Example of interface

A
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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Data Type of vars in interface

A

All variables declared in an interface automatically become

public static final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Default Methods in an Interface

A

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

17
Q

What is the Class Object

A
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
18
Q

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();
A

All

Tax myTax2 = new NJTax();
Object myTax3 = new NJTax();

are said to be upcasting

19
Q

Inheritance

A
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
{
}
20
Q

Method overriding

A

Ability to define some different functionality for a method than the functionality used in the superclass

21
Q

Where do you use method overriding

A
  1. Source code of superclass is not available
  2. Original version is still valid and needs to be kept intact
  3. To enable polymorphism
22
Q

When creating a class

A
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;
 }
23
Q

When inheriting from a super class -

A
Define the super constructor without paramters
public class meqCar extends car
{
 public meqCar()
 {
  super();
  }
}
24
Q

Characteristics of constructors

A
  1. Called when class is instantiated
  2. Must have the same name as the class they are in
  3. Can’t return a value and don’t specify void as return type
25
Explain static variable in class
``` static indicates that the class variable is shared by ALL instances of the same class. Ex: To count the instances of the class, you can have a static var; class Tax{ double grossIncome; static int customerCounter; // constructor public Tax() { customerCount++; } ```
26
How does a method declared as static behave
``` Methods declared as static can be called without the need to instantiate the class first. Usually done for utility methods that don't use any instance variables. ```
27
How do you create an array of Strings
String [] friends = new String [20] | String [] friends = {"John", "Doe", Doe1", "Doe2"}
28
Find the length of an Array
int totalElmnts = friends.length;
29
How to Convert an an array to String
toString(a) Ex: int [] a = {1,2,3}; System.out.println (Arrays.toString(a));
30
How to Sort an Array in ascending array
public static void sort(int[] a); | Ex: Arrays.sort(a)
31
How to Sort an Array in ascending order from an index to another
public static void sort(int[] a, int from, int to); | Ex: Arrays.sort(a, 0, 5)
32
How to perform a binary search in an array
public static int binarySearch(int[] a, int key) Ex: int found = Arrays.binarySearch(a, 5);
33
How to make a copy of an array
public static int[] copyOf(int[] original, int newLength) Ex: int[] a_copy = Arrays.copyOf(a, a.length) If smaller len is supplied, truncated copy created If larger len is supplied, padded copy created
34
How to copy a part of the array
public static int[] copyOfRange(int[] original, int from, int to) Ex: int[] a_copy = Arrays.copyOfRange(a, 0, 3);
35
How to fill an array
public static void fill(int[] a, int value) Ex: Arrays.fill(a, 0) Arrays.fill(a, 0, 3, 0) => From-To-Value
36
How to convert an Array to a List
public static List asList(int[] a); Ex: int[] a = { 1,2,3,4,5} List list1 = Arrays.asList(a)