Содержание
- 2. Lesson goals Class design Implement encapsulation Implement inheritance including visibility modifiers and composition Implement polymorphism
- 3. Object vs Class
- 4. Abstract Classes May contain any number of methods including zero If class has at least one
- 5. Method vs Constructor Method describes behavior Method signature: - name - arguments (including order) Specific method
- 6. Constructor class Animal { String name; Animal() { this("No-Name"); } public Animal(String name) { this.name =
- 7. Overloading and Overriding class Game { void play() { // move to the left } }
- 8. Overriding rules The access modifier must be the same or more accessible; The return type must
- 9. Overloading precedence Exact match by type Matching a superclass type Converting to a larger primitive type
- 10. Overloading precedence class Overloading { static void overloadedMethod(int i) { // will enter if nothing is
- 11. Interface Defines a set of public abstract methods, which classes implementing the interface must provide. Allows
- 12. Interface interface Walk { int someConst = 1; public static final int anotherConst = 1; boolean
- 13. Interface Provides a way for one individual to develop code that uses another individual’s code, without
- 14. Functional interface @FunctionalInterface interface Speakable { String say(); static void someStatic(){} default void someDefault(){} private void
- 15. Nested Classes Types of nested classes: A member inner class is a class defined at the
- 16. Nested Classes encapsulate helper classes by restricting them to the containing class make it easy to
- 17. Member Inner Classes class A { private int x = 1; class B { private int
- 18. Local Inner Classes class Outer { private int length = 5; public void calculate() { final
- 19. Anonymous Inner Classes public static void main(String[] args) throws Exception { JButton button = new JButton("red");
- 20. Static Nested Classes class Enclosing { static class Nested { private int price = 6; }
- 21. Enum A special data type that enables for a variable to be a set of predefined
- 22. Enum enum Planet implements IGravitable { MERCURY (3.303e+23, 2.4397e6) { @Override double surfaceGravity() { return -1;
- 23. Design principle A design principle is an established idea or best practice that facilitates the software
- 24. Design principle More logical code Code that is easier to understand Classes that are easier to
- 25. OOP principles Class Object Inheritance Encapsulation Polymorphism
- 26. OOP principles Everything is object Object is a class instance Program – a set of interacting
- 27. Access modifiers public class Dog { public String name = "Scooby-doo"; protected boolean hasFur = true;
- 28. Encapsulation
- 29. Encapsulation
- 30. Encapsulation No actor other than the class itself should have direct access to its data. The
- 31. Encasulation example class CoffeeMachine { private int sugar; private double milk; Coffee makeCoffee(){ blendBeans(); heatWater(); shakeMilk();
- 32. Encapsulation. Java Beans A JavaBean is a design principle for encapsulating data in an object in
- 33. Encapsulation. Java Beans What is wrong class Girl { private boolean playing; private Boolean dancing; public
- 34. Encapsulation. Java Beans What is wrong class Girl { private boolean playing; private Boolean dancing; public
- 35. Polymorphism An object in Java may take on a variety of forms, in part depending on
- 36. Polymorphism The type of the object determines which properties exist within the object in memory. The
- 37. Polymorphism interface LivesInOcean { void makeSound(); } class Dolphin implements LivesInOcean { public void makeSound() {
- 38. Polymorphism class Primate { public boolean hasHair() { return true; } } interface HasTail { boolean
- 39. Inheritance (is-a)
- 40. Inheritance (is-a) abstract class Animal { String name; @Override public String toString() { return name; }
- 41. Virtual methods invocation abstract class Animal { public abstract void feed(); } public void feedAnimal(Animal animal)
- 42. Composition (has-a) Object composition is the idea of creating a class by connecting other classes as
- 43. Composition (has-a) class Person { Job job; public Person() { this.job = new Job(); job.setSalary(1000L); }
- 44. Literature https://docs.oracle.com/javase/tutorial/java/javaOO/index.html https://docs.oracle.com/javase/tutorial/java/IandI/index.html https://docs.oracle.com/javase/tutorial/java/concepts/index.html
- 45. Homework Implement classes: SUV, Sedan, Hatchback3Doors, Hatchback5Doors SUV, Sedan, Hatchback3Doors, Hatchback5Doors should extend Vehicle Vehicle provides
- 47. Скачать презентацию