L4 + L5 - Defining Classes Flashcards
What is the central concept in Java that makes object-oriented programming (OOP) possible?
Classes: Classes define types and form the basis for creating objects, methods, and overall structure of Java programs.
How does Java programming typically organize software components?
Java programming consists of defining a number of classes. Each programmer-defined type in Java is implemented as a class, and even built in classes (like String and Scanner) are used to build software
What is the role of a class in Java?
A class serves as a blueprint for creating objects. It defines the properties (fields) and behaviors (methods) that the objects instantiated from the class will have.
Example of Predefined classes:
String and Scanner
What does it mean when we say “a class is a type” in Java?
It means that classes in Java are not only blueprints for objects but also serve as data types. When you create an object from a class, you’re creating an instance of that type.
How are programmer-defined types created in Java?
They are created by defining new classes. Every new type that a programmer defines in expressed as a class, which then can be used to instantiate objects.
What is the significance of classes in the structure of a Java program?
Classes are fundamental because they encapsulate data and behavior, making it possible to organize, reuse, and modularize code effectively. All software components can be thought of as collections of classes.
What is encapsulation?
The bundling of data (variables) and methods that operate on that data into a single unit, typically a class, while restricting direct access to some of the object’s details.
Describe the connection between objects and classes.
Objects are instances of classes. While a class is the abstract definition or blueprint, an object is a concrete instance that occupies memory and has state and behavior defined by its class.
Why is learning to define your own classes important for Java programming?
It allows you to create custom types that model real-world entities and problems. Defining your own classes is essential for building structured, modular, and maintainable programs.
What is one of the key differences between using a predefined class and user-defined class in Java?
With predefined classes, you use the functionality provided by Java (like String or Scanner), whereas with user-defined classes, you design and implement your own structures tailored to your specific application needs.
What is a class in Java?
A programmer-defined type that serves as a blueprint for creating objects, defining their attributes and behaviors
What are the two key components of a class definition?
Instance variables (fields) and methods.
How do you create an object in Java?
Using the ‘new’ operator. Example:
Classname obj - new Classname();
What is an instance variable?
A variable defined in a class that holds data for an object
How do you access an instance variable?
objectName.instanceVar;
What are the two types of methods in Java?
Void methods - Perform an action but do not return a value.
Return methods - Compute and return a value.
How do you invoke a method on an object?
Example:
objectName.methodName();
What is the purpose of the main method in Java?
It serves as the entry point of a Java program and has the signature:
public static void main(String[] args)
What is a constructor?
A special method used to initialize objects when they are created.
What are they key characteristics of a constructor?
- Has the same name as the class
- Does not have a return type
- Often overloaded to provide multiple initialization options
What happens if you do not define a constructor?
Java provides a default no-argument constructor.
What is the ‘this’ keyword used for?
Refers to the calling object and is used to differentiate instance variables from parameters.
What are accessor and mutator methods?
Accessor Methods - ( get methods ) retrieve instance variable values.
Mutator Methods ( set methods) modify instance variable values.