
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of java class loader and reflection, two essential concepts in java programming. Learn about the role of java class loader in creating and managing class objects, and discover how reflection api allows for dynamic inspection and manipulation of classes and objects at runtime. Explanations, examples, and practical usage of important reflection classes such as class, field, and method.
Typology: Slides
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























fields?
wants us to create and object of that class?
class Class { Constructor[] getConstructors(); Field getDeclaredField(String name); Field[] getDeclaredFields(); Method[] getDeclaredMethods(); ... }
class Field { Class getType(); ... }
class Method { Class[] getParameterTypes(); Class getReturnType(); ... }
class DemoClass { public String demoMethod(String demoParam) { System.out.println("Parameter passed: " + demoParam); return DemoClass.class.getName(); } }
public class DynamicClassLoadingExample {
public static void main(String[] args) { ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
// Step 2: Define a class to be loaded.
String classNameToBeLoaded = "net.viralpatel.itext.pdf.DemoClass";
// Step 3: Load the class
Class myClass = myClassLoader.loadClass(classNameToBeLoaded);
Abstract Classes and Interfaces
The objectives of this chapter are:
To explore the concept of abstract classes To understand interfaces To understand the importance of both.
What is an Abstract class?
Superclasses are created through the process called "generalization"
Common features (methods or variables) are factored out of object classifications (ie. classes). Those features are formalized in a class. This becomes the superclass The classes from which the common features were taken become subclasses to the newly created super class
Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world
It is an artifact of the generalization process
Because of this, abstract classes cannot be instantiated
They act as place holders for abstraction
What Are Abstract Classes Used For?
Abstract classes are used heavily in Design Patterns
Creational Patterns: Abstract class provides interface for creating objects. The subclasses do the actual object creation Structural Patterns: How objects are structured is handled by an abstract class. What the objects do is handled by the subclasses Behavioural Patterns: Behavioural interface is declared in an abstract superclass. Implementation of the interface is provided by subclasses.
Be careful not to over use abstract classes
Every abstract class increases the complexity of your design Every subclass increases the complexity of your design Ensure that you receive acceptable return in terms of functionality given the added complexity.
Defining Abstract Classes
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object
public abstract class Vehicle { private String make; private String model; private int tireCount; [...]
public class Car extends Vehicle { private int trunkCapacity; [...]
Vehicle
Car
Truck
public class Truck extends Vehicle { private int bedCapacity; [...]
Often referred to as "concrete" classes