0% found this document useful (0 votes)
9 views3 pages

06 Handout 1

Handout
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

06 Handout 1

Handout
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like