Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

IT1908 CONSTRUCTORS public Student() { name = "No name yet."; Fundamentals...

IT1908 CONSTRUCTORS public Student() { name = "No name yet."; Fundamentals age = 0; A constructor is used in the creation of an object that is an } instance of a class using the new keyword. public Student(String name, int age) { Ex. Employee emp = new Employee(); this.name = name; o A constructor performs operations required to this.age = age; initialize the class before methods are invoked or } fields are accessed. public static void main(String[] args) { Ex. public Employee() { Student s1 = new Student(); salary = 15000; System.out.println(s1.name + ", " + s1.age); } s1.name = "Riven Reyes"; Explanation: Every Employee object created will have s1.age = 17; a default starting salary of 15000 per month. System.out.println(s1.name + ", " + s1.age); o Constructors are never inherited. Student s2 = new Student("Nika Pena", 18); o Constructor declarations use the name of the class System.out.println(s2.name + ", " s2.age); and have no return type. } If you do not include any constructors in a class, Java provides } a default constructor, a constructor with an empty Explanation: Instances of the Student class can be created parameter list and body. This is invisibly added to the class. It with or without arguments. Student s1 = new Student(); is also known as a no-argument constructor. calls the default constructor because it does not have Ex. public Employee() { } arguments. The this keyword is used when the instance variable has the When a constructor calls another constructor with a greater same name with the constructor’s parameter. number of parameters, it is called constructor chaining. This Example: is accomplished using the this keyword too. The this() call public class Employee { must be the first statement in the constructor. private double salary; Example: public Employee(double salary) { public Student() { this.salary = salary; this("No name yet."); } } } public Student(String name) { this(name, 0); Constructor Overloading } Constructor overloading occurs when constructors have public Student(String name, int age) { different type parameters. this.name = name; Example: this.age = age; public class Student { } private String name; Reference: private int age; Oracle Docs (n.d.). Citing sources. Retrieved from https://docs.oracle.com/javase/tutorial/java/javaOO/index.html 04 Handout 1 *Property of STI  [email protected] Page 1 of 1 IT1908 ENCAPSULATION Ex. public boolean isEnrolled() { Fundamentals return status; Encapsulation describes the ability of an object to hide its } data and methods. 4. Names of setter methods begin with set. Benefits of encapsulation: Ex. public void setName(String name) { o It allows writing of reusable programs. this.name = name; o It restricts access only to those features of an object } that are declared public. In Java, a class encapsulates the fields, which holds the state Immutable Classes of an object, and the methods, which define the actions of the A class is considered immutable if it remains unchanged after object. an object of another class is constructed. Fields are encapsulated by declaring instance variables as For a class to be immutable, remove the setter methods and private and methods as public. use the constructor for setting the values. An accessor or a getter method is a public method that Example: returns data from a private instance variable. public class Student { A mutator or a setter method is a public method that changes private String name; the data stored in one or more private instance variables. public Student(String name) { Example: this.name = name; public class Student { } private String name; public String getName() { public void setName(String name) { return name; this.name = name; } } } public String getName() { return name; References: Savitch, W. (2014). Java: An introduction to problem solving and programming (7th ed.). } New Jersey: Pearson Education, Inc. } Oracle Docs (n.d.). Citing sources. Retrieved from https://docs.oracle.com/javase/tutorial/java/javaOO/index.html Rules in Implementing Encapsulation 1. Instance variables are declared private. Ex. private String name; 2. Names of getter methods begin with get if the property is not a boolean. Ex. public String getName() { return name; } 3. Names of getter methods begin with is if the property is a boolean. 05 Handout 1 *Property of STI  [email protected] Page 1 of 1 IT1908 INHERITANCE In Java, all classes inherit from the Object class. It is the only class that does not have any parent classes. Fundamentals //Class declaration 1 Inheritance allows a class to acquire all the attributes (fields) public class Student { } and behaviors (methods) of another class. //Class declaration 2 A class that is derived from another class is called a public class Student extends java.lang.Object { } subclass. It is also known as derived class or child class. Explanation: Both class declarations mean the same. A class The class from which the subclass is derived is called a that does not extend another class invisibly extends the superclass. It is also known as base class or parent class. Object class. Example: To prevent a class from being extended, mark the class with //Person.java the final modifier. public class Person { Even though the superclass is public, its subclass cannot private String name; access its private fields. public void setName(String name) { Example: this.name = name; //Person.java } public class Person { public String getName() { private String name; return name; public void setName(String name) { } this.name = name; } } //Student.java public String getName() { public class Student extends Person { return name; public static void main(String[] args) { } System.out.println(getName()); } } //Student.java } public class Student extends Person { Explanation: Student is the subclass while Person is the public static void main(String[] args) { superclass. Assuming that both classes are in the same System.out.println(name); //does not compile package, an import statement is not required in Student.java } to access the Person class. } Java supports multiple levels of inheritance where a class may Explanation: The name variable in the Person class is marked extend another class, which in turn extends another class. as private and therefore not accessible from the subclass Example: Student. public class Person { } public class Student extends Person { } Rules in Defining Constructors public class FirstYear extends Student { } 1. The first statement of every constructor is either a call to Java supports single inheritance where a class may inherit another constructor within the class using this() or a call to from only one (1) direct parent class. Extending multiple a constructor in the direct parent class using super(). classes (multiple inheritance) are not allowed. 06 Handout 1 *Property of STI  [email protected] Page 1 of 3 IT1908 Example: //Equivalent to the empty constructor //Person.java public Student() { public class Person { super(); private String name; } public Person(String name) { Explanation: The Java compiler invisibly adds a no-argument this.name = "New student; constructor super() if the first statement is not a call to the } parent constructor. } 4. If the parent class does not have a no-argument constructor //Student.java and the child class does not define any constructors, the public class Student extends Person { compiler will throw an error. This is because the child class public Student(String name) { //1st constructor has an invisible no-argument super() that tries to call the super(name); constructor of the parent class. } Example: public Student() { //2nd constructor //Person.java this("No name yet."); public class Person { } private String name; } public Person(String name) { Explanation: In the Student class, the first statement of the this.name = name; first constructor is a call to the constructor of the Person class. } It also includes another no-argument constructor that calls the } other constructor within the class using this("No name //Student.java yet."). public class Student extends Person { } 2. The super() command may only be used as the first //above does not compile statement of the constructor. Explanation: The compiler adds a no-argument constructor Example: and a no-argument super(). However, the Person class public Person() { does not have a no-argument constructor, leading to a System.out.println("Person object created."); compilation error. super(); //does not compile 5. If the parent class does not have a no-argument constructor, } the compiler requires an explicit call to a parent constructor in Explanation: The super() command is the second statement each child constructor. and therefore will lead to a compilation error. Example: 3. If a super() call is not declared in a constructor, Java will //Person.java insert a no-argument super() as the first statement of the public class Person { constructor. private String name; Example: public Person(String name) { //Empty Constructor this.name = name; public Student() { } } } 06 Handout 1 *Property of STI  [email protected] Page 2 of 3 IT1908 //Student.java The code will execute with the parent constructors called first, public class Student extends Person { producing the output below. public Student(){ Output: Person super("Nika Pena"); Student } You can define a new version of an existing method in a child } class that makes use of the definition in the parent class. This Explanation: The constructor in Person can only be called if ability is called method overriding. the Student specifies a String argument in the super() call To override a method, declare a new method with the same of its constructor such as super("Nika Pena"); name, parameter list, and return type as the method in the parent class. The method in the subclass must be at least as Calling Constructors and Overriding Methods accessible as the method in the parent class. The parent constructor is always executed before the child Example: constructor. //FirstClass Example: public class FirstClass { //Person.java protected String getMessage() { return "Method"; public class Person { } public Person() { } System.out.println("Person"); //SecondClass } public class SecondClass extends FirstClass { } public String getMessage() { //Student.java return super.getMessage() + " Overriding"; public class Student extends Person { } public Student() { public static void main(String[] args) { System.out.println("Student"); System.out.println(new SecondClass().getMessage()); } } } } Explanation: The getMessage() method of SecondClass //FirstYear.java overrides getMessage() of FirstClass. The super keyword public class FirstYear extends Student { can be used to access the method of the parent class being public static void main(String[] args) { overridden. FirstYear fy = new FirstYear(); Output: Method Overriding } } Output: Person Student Explanation: The compiler first inserts invisible super() commands as the first statements of both Person and References: Student constructors. Next, the compiler inserts an invisible Savitch, W. (2014). Java: An introduction to problem solving and programming (7th ed.). default no-argument constructor in the FirstYear class with New Jersey: Pearson Education, Inc. Oracle Docs (n.d.). Citing sources. Retrieved from an invisible super() as the first statement of the constructor. https://docs.oracle.com/javase/tutorial/java/javaOO/index.html 06 Handout 1 *Property of STI  [email protected] Page 3 of 3

Use Quizgecko on...
Browser
Browser