java short cuts for the beginners, Cheat Sheet of Earth science

Designed for students in the 400-level Artificial Intelligence course (CPE 421) , Faculty of Engineering, this material is aligned with the NUC curriculum and prepares students for both academic assessments and real-world applications.

Typology: Cheat Sheet

2024/2025

Uploaded on 04/17/2025

abass-oguntade
abass-oguntade 🇿🇦

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java OOP Concepts - Exam Revision
1. Inheritance (IS-A Relationship)
Allows a child class (subclass) to inherit properties (fields/methods) from a parent class
(superclass).
- Keyword: extends
- Advantage: CR (Code Reusability)
- Disadvantage: TC (Tightly Coupled Classes)
- Types:
- Single Inheritance (One parent -> One child)
- Multilevel Inheritance (Parent -> Child -> Grandchild)
Example:
class Parent {
void show() { System.out.println("Parent class method"); }
}
class Child extends Parent {
void display() { System.out.println("Child class method"); }
}
public class Main {
public static void main(String args[]) {
Child obj = new Child();
obj.show(); // Inherited method
obj.display(); // Own method
}
}
2. Method Overriding (Runtime Polymorphism)
Subclass provides its own version of a method already defined in the superclass.
- Condition: Method signature must be the same.
- Keyword: @Override (optional but recommended)
- Achieves: RP (Runtime Polymorphism)
Example:
class Parent {
void show() { System.out.println("Parent method"); }
}
pf3
pf4

Partial preview of the text

Download java short cuts for the beginners and more Cheat Sheet Earth science in PDF only on Docsity!

Java OOP Concepts - Exam Revision

1. Inheritance (IS-A Relationship)

Allows a child class (subclass) to inherit properties (fields/methods) from a parent class (superclass).

  • Keyword: extends
  • Advantage: CR (Code Reusability)
  • Disadvantage: TC (Tightly Coupled Classes)
  • Types:
    • Single Inheritance (One parent -> One child)
    • Multilevel Inheritance (Parent -> Child -> Grandchild)

Example:

class Parent { void show() { System.out.println("Parent class method"); } } class Child extends Parent { void display() { System.out.println("Child class method"); } } public class Main { public static void main(String args[]) { Child obj = new Child(); obj.show(); // Inherited method obj.display(); // Own method } }

2. Method Overriding (Runtime Polymorphism)

Subclass provides its own version of a method already defined in the superclass.

  • Condition: Method signature must be the same.
  • Keyword: @Override (optional but recommended)
  • Achieves: RP (Runtime Polymorphism)

Example:

class Parent { void show() { System.out.println("Parent method"); }

class Child extends Parent { @Override void show() { System.out.println("Child method"); } } public class Main { public static void main(String args[]) { Parent obj = new Child(); obj.show(); // Child method runs (Overriding) } }

3. Polymorphism (Many Forms)

Same method name behaves differently based on context.

  • Types:
    • Compile-time Polymorphism (Static) - Method Overloading
    • Runtime Polymorphism (Dynamic) - Method Overriding

Example:

class MathOps { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }

4. Abstraction (Hiding Details)

Shows only necessary details while hiding implementation.

  • Achieved using:
    • Abstract Class (abstract keyword)
    • Interface (interface keyword)
  • 100% Abstraction possible only in Interfaces.

Example (Abstract Class):

abstract class Animal { abstract void makeSound(); } class Dog extends Animal { void makeSound() { System.out.println("Bark"); }

7. String Methods (10 Examples)

Common Methods:

  • length() - Finds string length
  • charAt(index) - Returns character at index
  • substring(start, end) - Extracts part of a string
  • toUpperCase() - Converts to uppercase
  • toLowerCase() - Converts to lowercase
  • replace(old, new) - Replaces characters
  • contains(str) - Checks if string contains a sequence
  • equals(str) - Compares two strings
  • trim() - Removes whitespace
  • split(delimiter) - Splits string into array

Example:

public class StringDemo { public static void main(String args[]) { String str = " Hello Java! "; System.out.println("Original: " + str); System.out.println("Length: " + str.length()); System.out.println("Trimmed: " + str.trim()); System.out.println("Uppercase: " + str.toUpperCase()); System.out.println("Character at 1: " + str.charAt(1)); System.out.println("Substring: " + str.substring(2, 7)); System.out.println("Replaced: " + str.replace("Java", "World")); } }

KEY TAKEAWAYS (Quick Memory Hack)

  1. Inheritance -> 'IS-A' (Extends) -> CR (Code Reusability)
  2. Method Overriding -> 'Same Method, Different Class' -> RP (Runtime Poly.)
  3. Polymorphism -> 'Many Forms' -> Compile-time & Runtime
  4. Abstraction -> 'Hides Details' -> Abstract Classes & Interfaces
  5. Encapsulation -> 'Capsules Data' -> Private Vars + Getters/Setters
  6. I/O Streams -> Scanner (Input) & System.out (Output)
  7. String Methods -> 'length(), trim(), replace(), substring()'