• Writing Final Classes and Methods• Abstract Methods and Classes... Defining an Interface 2public class TestSimpleTimeClient { public static void mainString..... Inheritance 3public cl
Trang 1Session 05 Interface and Inheritance
(https://docs.oracle.com/javase/tutorial/java/IandI/)
Trang 2• Writing Final Classes and Methods
• Abstract Methods and Classes
Trang 3Interface in Java
• An interface is a reference type, similar to a
class, that can contain only constants, method
signatures, default methods, static methods, and nested types
• Interfaces cannot be instantiated
• Interfaces can only be implemented by classes
or extended by other interfaces.
Trang 4Defining an Interface
public interface TimeClient {
void setTime(int hour, int minute, int second); void setDate(int day, int month, int year);
void setDateAndTime(int day, int month, int
year, int hour, int minute, int second);
LocalDateTime getLocalDateTime();
}
Trang 6Defining an Interface (2)
public class TestSimpleTimeClient {
public static void main(String args) { TimeClient myTimeClient = new
Trang 7Implementing Object-Oriented
Relationships
• There are two phrases that are
commonly used when describing a class
in plain English:
– “is a”: describe the superclass
– “has a”: describe the member variables
• For example, consider this description:
– “A home is a house that has a family and a pet.”
Trang 8+ String getStudentId();
Trang 9Inheritance (1)
• An inheritance is a relationship where objects share a common structure: the structure of one object is a sub-structure of another
object
Trang 10Inheritance (2)
• In object-oriented programming, we describe inheritance in terms of classes Inherited
classes share a common structure We say
that one class is a kind of another class
• For example, the Student class is a kind of
Person class
Trang 11Inheritance (3)
• The subclass inherits the structure of a
superclass
• For example, a student inherits the structure
of a person Person is the superclass, while
Student is the subclass.
Trang 12• If a class does not have any superclass, then it
is implicitly derived from Object class
• Unlike other members, constructor cannot be inherited
Trang 13Inheritance (3)
public class Bicycle {
public int cadence;
public int gear;
public int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
Trang 14Inheritance (3)
public class MountainBike extends Bicycle {
// the MountainBike subclass adds one field
public int seatHeight;
// the MountainBike subclass has one constructor public MountainBike(int startHeight,
// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
Trang 15Overriding and Hiding Methods (1)
• An instance method in a subclass with the
same signature (name, plus the number and the type of its parameters) and return type as
an instance method in the superclass
overrides the superclass's method
• Use the @Override annotation that instructs the compiler that you intend to override a
method in the superclass
Trang 16Overriding and Hiding Methods (2)
public class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal");
}
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
Trang 17Polymorphism (1)
• The ability of two or more objects belonging
to different classes to respond to exactly the
same message (method call) in different
class-specific ways.
• Inheritance combined with overriding
facilitates polymorphism.
Trang 19s2.print();
s3.print();
}
Trang 20“super” Keyword (1)
• We use the Java keyword super as the
qualifier for a method call:
super methodName(arguments);
• Whenever we wish to invoke the version of
method methodName that was defined by our superclass
• super() is used to access the superclass's
constructor And It must be the first statement
in the constructor of the subclass
Trang 21“super” Keyword (2)
• Accessing Superclass Members
public class Subclass extends Superclass {
// overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
Trang 22“super” Keyword (3)
• Constructors Are Not Inherited
• super( ) for Constructor Reuse
• The call must be the first statement in the
subclass constructor
• Replacing the Default Parameterless
Constructor
Trang 23Object as a Superclass
• The Object class, in the java.lang package, sits at the top of the class hierarchy tree
• Every class is a descendant, direct or indirect, of
the Object class
• Every class you use or write inherits the instance
methods of Object.
• Some methods inherited from Object:
• public String toString()
• public boolean equals(Object obj)
• …
Trang 24Final Classes and Methods
• final keyword in a method declaration to indicate
that the method cannot be overridden by subclasses.
• A class that is declared final cannot be subclassed.
Trang 25Abstract Classes (1)
• Used to define what behaviors a class is required to
perform without having to
provide an explicit implementation.
• Syntax to define a abstract class
• public abstract class className{ }
class to be abstract.
methods
Trang 27Implementing Abstract Methods
• Derive a class from an abstract superclass, the subclass will inherit all of the superclass’s
features, all of abstract methods included
• To replace an inherited abstract method with
a concrete version, the subclass need merely
override it.
• Abstract classes cannot be instantiated
Trang 28• Writing Final Classes and Methods
• Abstract Methods and Classes