1 / 8

Understanding Interfaces and Inheritance in Java: A Comprehensive Overview

This guide dives into the concepts of interfaces and inheritance in Java programming. You'll learn how to define interfaces using `public interface`, implement them in classes, and extend existing interfaces to create complex functionality. We explore practical examples like `OperateCar` and `MountainBike` to demonstrate method signatures, the implementation of methods, and the importance of polymorphism with abstract classes. Additionally, this overview includes type casting and provides insights into best practices when working with interfaces and inheritance in Java.

valora
Download Presentation

Understanding Interfaces and Inheritance in Java: A Comprehensive Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Интерфейсы и наследование

  2. Интерфейсы • Объявление • public interface OperateCar { • // constant declarations, if any • // method signatures • intturn(Direction direction, double radius, double startSpeed); • intchangeLanes(Direction direction, double startSpeed, double endSpeed); • } • Реализация • public class OperateBMW760i implements OperateCar { • // the OperateCar method signatures, with implementation ,for example: • intsignalTurn(Direction direction, booleansignalOn) { • // code to turn BMW's LEFT turn indicator lights on • // code to turn BMW's LEFT turn indicator lights off • } • // other members, as needed -- for example, helper classes not // visible to clients of the interface • }

  3. Расширение • Интерфейсов public interface GroupedInterface extends Interface1, Interface2, Interface3 { // constant declarations // base of natural logarithms double E = 2.718282; // method signatures void doSomething (int i, double x); intdoSomethingElse(String s); }

  4. Наследование public class Bicycle { //implement } public class MountainBike extends Bicycle { // the MountainBike subclass adds one field public intseatHeight; // the MountainBike subclass has one constructor public MountainBike(intstartHeight, intstartCadence, intstartSpeed, intstartGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } }

  5. Приведениетипов MountainBikemyBike = new MountainBike(0, 0, 0, 0); Object obj = new MountainBike(0, 0, 0, 0); MountainBikemyBike1 = (MountainBike)obj; if (objinstanceofMountainBike) { MountainBikemyBike2 = (MountainBike)obj; }

  6. Полиморфизм ДемоTestBike

  7. Final и Астрактны Классы • Final final class ChessAlgorithm { enumChessPlayer { WHITE, BLACK } ... finalChessPlayergetFirstPlayer() { return ChessPlayer.WHITE; } ... } • Abstract public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }

  8. Q&A

More Related