Lecture Software construction - Lecture 7: Java implementation

Lecture Software construction - Lecture 7: Java implementation. The contents of this chapter include all of the following: Interfaces in java, reference data types, abstract classes in Java, overview of generics in Java. | Java Implementation 2 Software Construction Lecture 7 1 Agenda 2 Topics: Interfaces in Java Reference data types Abstract classes in Java Overview of generics in Java Reading In The Java Tutorials: What is an Interface?, in the Object-Oriented Programming Concepts Lesson The Interfaces and Inheritance Lesson Why Use Generics? in the Generics (Updated) Lesson. Abstract Classes 3 Sometimes, it’s appropriate to partly-implement a class or interface. Abstract classes allow code to be reused in similar implementations. Abstract classes may include some abstract methods. If there are no abstract methods, then the class is usually (but not always) implemented fully enough to be used by an application. Sometimes it’s helpful to have multiple implementations that differ only in their type, but this is quite an advanced concept in design. public abstract class MyGraphicObject { // declare fields – these may be non-static private int x, y; // declare non-abstract methods // (none) // declare . | Java Implementation 2 Software Construction Lecture 7 1 Agenda 2 Topics: Interfaces in Java Reference data types Abstract classes in Java Overview of generics in Java Reading In The Java Tutorials: What is an Interface?, in the Object-Oriented Programming Concepts Lesson The Interfaces and Inheritance Lesson Why Use Generics? in the Generics (Updated) Lesson. Abstract Classes 3 Sometimes, it’s appropriate to partly-implement a class or interface. Abstract classes allow code to be reused in similar implementations. Abstract classes may include some abstract methods. If there are no abstract methods, then the class is usually (but not always) implemented fully enough to be used by an application. Sometimes it’s helpful to have multiple implementations that differ only in their type, but this is quite an advanced concept in design. public abstract class MyGraphicObject { // declare fields – these may be non-static private int x, y; // declare non-abstract methods // (none) // declare methods which must be implemented later abstract void draw(); } 4 public class Rectangle extends Shape { private int width, height; public int area() { return (width * height); } . public abstract class Shape { public abstract void draw(Graphics g); public abstract int area(); } public class Triangle extends Shape { private int width, height; public int area() { return (width * height) / 2; } . The method signature is declared, but no implementation is provided. Abstract methods Provide implementation in subclasses public class Circle extends Shape { private int radius; public int area() { return (int) ( * radius * radius); } . Final 5 The final keyword can be applied to prevent the extension (over-riding) of a field, argument, method, or class. Final field: constant Final argument: cannot change the data within the called method Final method: cannot override method in subclasses Final class: cannot be subclassed (all of its methods are implicitly final as well) class .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.