OOP Midterm Reviewer PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document is a midterm reviewer for an object-oriented programming class. It covers key concepts such as constructors, encapsulation, inheritance and immutable classes. The document appears to be in a handout format.
Full Transcript
OOP MIDTERM REVIEWER ACCESSOR - An accessor or a getter method is a public method that returns data from a MULTIPLE CHOICES private instance variable....
OOP MIDTERM REVIEWER ACCESSOR - An accessor or a getter method is a public method that returns data from a MULTIPLE CHOICES private instance variable. MUTATOR - A mutator or a setter method is a HANDOUT 1 public method that changes the data stored in one or more private instance variables. CONSTRUCTOR - is used in the creation of an object that is an instance of a class using the RULES IN IMPLEMENTING ENCAPSULATION new keyword. Constructors are never inherited. 1. Instance variables are declared private. Constructor declarations use the name 2. Names of getter methods begin with get of the class and have no return type. if the property is not a boolean. DEFAULT CONSTRUCTOR - If you do not 3. Names of getter methods begin with is if include any constructors in a class, Java the property is a boolean provides a default constructor. 4. Names of setter methods begin with set. NO ARGUMENT CONSTRUCTOR - This is IMMUTABLE CLASS invisibly added to the class. It is also known as a no-argument constructor. IMMUTABLE - A class is considered immutable if it remains unchanged after an object of CONSTRUCTOR OVERLOADING - occurs another class is constructed. when constructors have different type For a class to be immutable, remove parameters. the setter methods and use the constructor for setting the values. CONSTRUCTOR CHAINING - When a constructor calls another constructor with a greater number of parameters, it is called HANDOUT 3 constructor chaining. INHERITANCE - Inheritance allows a class to acquire all the attributes (fields) and behaviors HANDOUT 2 (methods) of another class. ENCAPSULATION – describes the ability of an SUBCLASS - A class that is derived from object to hide its data and methods. another class is called a subclass. It is also known as derived class or child class. Benefits of encapsulation: The class from which the subclass is It allows writing of reusable programs. derived is called a superclass. It is also It restricts access only to those features of an object that are declared public. known as base class or parent class. In Java, a class encapsulates the fields, which hold the state of an object, and RULES IN DEFINING CONSTRUCTORS the methods, which define the actions of the object. 1. The first statement of every constructor is either a call to another Fields are encapsulated by declaring constructor within the class using instance variables as private and this() or a call to a constructor in the methods as public. direct parent class using super(). 2. The super() command may only be } used as the first statement of the constructor. INHERITANCE 3. If a super() call is not declared in a constructor, Java will insert a public class Person { no-argument super() as the first statement of the constructor. private String name; 4. If the parent class does not have a no-argument constructor and the child public void setName(String name) { class does not define any constructors, the compiler will throw this.name = name; an error. This is because the child class has an invisible no-argument } super() that tries to call the constructor of the parent class. public String getName () { 5. If the parent class does not have a no-argument constructor, the compiler return name; requires an explicit call to a parent constructor in each child constructor. } CALLING CONSTRUCTORS AND OVERRIDING METHODS } The parent constructor is always public class Student extends Person executed before the child constructor. { You can define a new version of an existing method in a child class that public static void main (String [] makes use of the definition in the args) { parent class. This ability is called method overriding. System.out.println(getName()); To override a method, declare a new method with the same name, parameter } list, and return type as the method in the parent class. The method in the } subclass must be at least as accessible as the method in the parent class. CALLING PARENT CLASS CONSTRUCTOR IN THE CHILD CONSTRUCTOR TEST lll //PERSON.JAVA CLASS WITH CONSTRUCTOR public class Person { public Person () { public Employee () { System.out.print (“Person”); salary = 15000; } } } //STUDENT.JAVA public public class Student extends Person { classEmployee { public Student () { private double System.out.println (“Student”); salary; } } //FIRSTYEAR.JAVA public Employee(double salary) { public class FIRSTYEAR extends Student this.salary = salary; public static void main (String [] args) { FirstYear fy = new FirstYear (); } } }