1/28
encapsulation, inheritance, polymorphism, abstraction and important concepts
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
Inheritance
Allows a new class to reuse and extend existing class code
Abstraction
A way of hiding complex details of how something works and only showing essential features
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
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
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
If you want to access a class from another package:
You use the import keyword at the top of the file
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
Constructor
A special method used to initialise objects
Correct class syntax:
public class Student{}
Which keyword allows a classes own method to access its private attributes?
this
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
Super keyword
Refers to the superclass constructor or method
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
What class do all Java classes inherit from?
Object class
Object Oriented Programming
A programming paradigm that organises code into objects that combine attributes and methods
Promotes modularity, reusability and easier code maintanance
What is a class?
A blueprint from which individual objects are created
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
Attributes
Access modifier → public, private, protected
Data type → int, string, double etc
Attribute name → camelCase
Methods
accessModifier returnType methodName(paramaterList){
//method body
Return value; //if not void
}
Instantiation
The process of creating an object from a class
Creates a unique copy of the class blueprint
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
Advantages of Encapsulation
Data protection → getters
Controlled access → setters
Hides complexity and implementation details → just calls methods
String gatekeepers:
.trim()
.isEmpty()
.toLowerCase()
.matches(String regex)
.charAT(int index) and .indexOf(Char)
.contains(String str)
.split(String
Scanner Class
Pre defined class
Part of java.util package
allows program to read input from different sources
Keyboard input
Files
Strings
Scanner class methods:
nextLine()
nextBoolean()
nextLong()
nextShort()
nextByte()
etc
Example of Polymorphism with Inheritance
Animal a = new Dog();
a.makeSound();
This would execute the dog version of the makeSound() at run time
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’