OOP Java Revision

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/28

flashcard set

Earn XP

Description and Tags

encapsulation, inheritance, polymorphism, abstraction and important concepts

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

29 Terms

1
New cards

Encapsulation

A way of grouping data and methods into a single object

Allows for greater security and simplifying data hiding

Prevents uncontrolled access to object data

2
New cards

Inheritance

Allows a new class to reuse and extend existing class code

3
New cards

Abstraction

A way of hiding complex details of how something works and only showing essential features

4
New cards

Polymorphism

The ability of an object or method to take many forms

Allows the same method to perform different actions depending on the object it belongs to

Lets one interface work with many different objects

5
New cards

Object Types - Important concept

Objects created from subclasses can be treated as objects of their parent class

Basis of polymorphism and type hierarchy in OOP

6
New cards

Why is OOP easier to maintain as a project grows?

OOP uses encapsulation, inheritance and Polymorphism to make reusable, modular classes which make larger systems easier to update and extend

7
New cards

If you want to access a class from another package:

You use the import keyword at the top of the file

8
New cards

How do JDK, JRE and JVM work together when running a program?

  • JDK (Java Development Kit) provides the tools to compile and run programs

  • JRE (Java Runtime Environment) contains libraries needed to run them

  • JVM (Java Virtual Machine) executes the compiled byte code on the host machine

9
New cards

Constructor

A special method used to initialise objects

10
New cards

Correct class syntax:

public class Student{}

11
New cards

Which keyword allows a classes own method to access its private attributes?

this

12
New cards

What would happen if all attributes in a class were declared public and without getters/setters?

Encapsulation would be broken

Any external code would be able to change data

It would be harder to control and protect from invalid values

13
New cards

Super keyword

Refers to the superclass constructor or method

14
New cards

What keyword is recommended over an override method and why?

  • @Override

  • Tells the compiler that the method overrides the superclass method

  • Prevents errors if the signatures don’t match

15
New cards

What class do all Java classes inherit from?

Object class

16
New cards

Object Oriented Programming

  • A programming paradigm that organises code into objects that combine attributes and methods

  • Promotes modularity, reusability and easier code maintanance

17
New cards
18
New cards

What is a class?

A blueprint from which individual objects are created

19
New cards

Method Overloading

  • allows multiple methods in the same class to have the same name but different parameter lists

  • enables a single method to perform similar actions on different kinds of data

20
New cards

Attributes

  • Access modifier → public, private, protected

  • Data type → int, string, double etc

  • Attribute name → camelCase

21
New cards

Methods

accessModifier returnType methodName(paramaterList){

//method body

Return value; //if not void

}

22
New cards

Instantiation

  • The process of creating an object from a class

  • Creates a unique copy of the class blueprint

23
New cards

Java Access Modifiers

  • Control visibility of classes, variables and methods

  • Helps enforce encapsulation

  • 4 main access levels:

    • Public + → visible everywhere

    • Private - → visible in same package and subclasses

    • Protected # → only visible in same class

24
New cards

Advantages of Encapsulation

  • Data protection → getters

  • Controlled access → setters

  • Hides complexity and implementation details → just calls methods

25
New cards

String gatekeepers:

  • .trim()

  • .isEmpty()

  • .toLowerCase()

  • .matches(String regex)

  • .charAT(int index) and .indexOf(Char)

  • .contains(String str)

  • .split(String

26
New cards

Scanner Class

  • Pre defined class

  • Part of java.util package

  • allows program to read input from different sources

    • Keyboard input

    • Files

    • Strings

27
New cards

Scanner class methods:

  • nextLine()

  • nextBoolean()

  • nextLong()

  • nextShort()

  • nextByte()

  • etc

28
New cards

Example of Polymorphism with Inheritance

Animal a = new Dog();

a.makeSound();

This would execute the dog version of the makeSound() at run time

29
New cards

What happens when you execute:

String s1 = “Hi”;

String s2 = s1;

s1 = “Bye”;

  • Strings are immutable

  • initially s1 and s2 both point to the ‘Hi’ object

  • After s1 = “Bye” runs, s1 points to a new string

  • s2 still points to ‘Hi’