Java 2 Flashcards

1
Q

Nested Classes:

public class FlightTicket {

public static class Business {

}

public static class Economy {

}

public static class First {

	} }

how do i create an object of any of the nested classes?

A

FlightTicket.Business business = new FlightTicket.Business();

etc.

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

Inheritance principles: (7 in total)

A
  1. inherits properties/variables and behavior/methods of another class
  2. Great for code Reusability and Maintainability
    Example of this: When i have to set up API i can extend the properties such as the BaseURI of the SpartanBase Class
  3. have to use extends keyword
  4. IS-A relation between classes
    If Vehicle is parent class then
    Car IS-A Vehicle
    Bus IS-A Vehicle
  5. Can only extend one class
  6. Constructors are NOT Inherited
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Inheritance: Method Overloading

A

-2 methods with the same name and parameters
- Child class can have a different implementation of the same method signature as the parent.
- Method in child class must be at least as accessible or more accessible as the method in parent class.
- Therefore, cannot override a public method in parent class with a method that is private. etc.
- Method return must be the same of a covariant.
Example: A method that returns a person can be overridden with a method that returns student since Student IS-A Person

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Inheritance: Child Class
Can a child class use Parents Variables or methods?
A
  • Can have its own instance variables and methods
  • Can use parents variables and methods using:
  • super.instanceVariable
  • super.instanceMethod
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Method hiding:

A
  • There is a difference between hiding a static method and overriding.
    The version of the overridden instance method that gets invoked is the one in the subclassthe version of the hidden static method that gets invoked depends on where it is invoked from the superclass or subclass.

if the child class defines the method as static with same method signature as parent class, then the method in the child class hides the one in parent class.

Example:
Parent Class:
			public static void testClassMethod(){
			Sysout(" A - Parent")
			}
			public void testInstanceMethod(){
			Sysout(" B - Child")
			}
Child Class:
			public static void testClassMethod(){
			Sysout(" A - Child ")
			}
			public void testInstanceMethod(){
			Sysout(" B - Child ")
			}
Parent parent = new Parent();
Child myChild = parent;

Child.testClassMethod(); // Sysout(“ A - Parent”)
myChild.testInstanceMethod; //Sysout(“ B - Child “)

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

Multi-Level Inheritance
Hierarchical Inheritance
Multiple Inheritance

A
  • Child class can extend itself to another class
  • One class can serve as parent for many child classes
  • Java only allows you to extend one class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does inheritance work given the same/different packages when it comes to access modifiers?

A

In same package:

  • public
  • protected
  • default
  • private is never inherited no matter what

Different Package:

  • public
  • protected
  • default
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Inheritance: Super Keyword

A

Super keyword is used to call the constructor

  • it can call any of the constructor of the parent as long as the same parameters are given.
  • has to be the first line of the code
  • benefits: reusability logic, no new code is needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Final class

A

cannot be extended, cannot be inherited

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

Final method

A

cannot be overriden in sub class

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

Final Variable

A

Cannot be reassigned to a new value

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

Access modifiers, what are they and explain each one.

A

Private > default > protected > public

private: is only visible/accessible in same class. Private method and variables are not inherited

default- only visible/accessible in same package only. Not visible or accessible to any other class in different package. default variables/methods are only when sub class and parent class are in the same package

protected - visible/accessible in same package or sub classes in different packages

public visible/accessible to very class, in same package or another. all variables and methods are inherited

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

Constructor and Inheritance

A

When we have a parent-child relation between two classes and we make an object of the child class. The parent’s constructor will run first and create an object of the Parent then of the child

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

Abstraction - OOP

A

2 Ways to use abstraction:

  • Abstract class
  • Interface
  • Abstract classes can be extended to child classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What keyword has to be at the top of the class for it to be considered abstract?

A
abstract keyword must be added to class Name
ex: abstract class Animal {

}

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

Can abstract classes be instantiated to a object ?

A

Cannot create objects of abstract classes

17
Q

In the abstract what can/cannot do for method

A

Only declare method but cannot implement any code
implement will be done in child class
- Sub classes are called concrete class

18
Q

can an abstract class contain abstract methods and non-abstract methods

A

Yes they can

19
Q

Major Benefit of Abstract class

A

To store and reuse variables and methods and do a unique implementation in each child class

20
Q

Abstraction - Interface type

A

Must have the interface keyword on the top
- public interface Teach{

}

21
Q

Can you extend multiple interfaces ?

Can you create an object of Interface class?

A

Yes you can, its one of the benefits of interface

No you cannot create object

22
Q

For an interface class, can you have a constructor?

A

No you cannot have a constructor for interface class

23
Q

Rules of an Interface class

A
  1. Default methods can only exist in an interface
  2. default keyword is mandatory to declare a default method
  3. default methods cannot be static, final or abstract
  4. methods are automatically public and cannot be changed
24
Q

Static keyword in interface

A
- static method was added since java 8
public interface Teachable {
			public abstract void canLean();
			public static void practice(){
			 //implementation code here
			 }
			}
25
Q

What are the only fields accepted in interface class?

A
Yes, an Interface class can only have constant variables
public static final int CONSTANT = 10;
26
Q

How do you inherit and interface class?

A

Must use the implement keyword. Can implement multiple interfaces

27
Q

In an interface class are methods are…

A

public abstract