Java
/
OOP
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Casting
p213 In Java polymorphism is the ability of an object to take many forms.
/**
* In Java the object can be different types (polymorphism).
*
* To access a subtype method from a super type object ...
* you must cast the object with subtype.
*/
package com.minte9.oop.polymorphism;
public class Casting {
public static void main(String[] args) {
Animal a1 = new Dog(); // reference is an Animal
Animal a2 = new Cat(); // object is a Cat
System.out.println(a1.roaming());
System.out.println(a2.eating());
System.out.println((
(Dog) a1).barking() // Look Here
);
// myDog is roaming (super method)
// myCat is eating (super method)
// myDog is barking (Dog method)
}
}
class Animal {
protected String name;
public String eating() {
return name + " is eating (super method)";
}
public String roaming() {
return name + " is roaming (super method)";
}
}
class Dog extends Animal {
public Dog() {
name = "myDog";
}
public String barking() {
return name + " is barking (Dog method)";
}
}
class Cat extends Animal {
public Cat() {
name = "myCat";
}
}
Parameter
You can declare the method parameter as a super-class type.
/**
* Polymorphism and Dependency Injection
*
* Using polymorphism we can pass object as parameters ...
* and call object's super methods.
*/
package com.minte9.oop.polymorphism;
public class Parameter {
public static void main(String[] args) {
File file = new File();
Csv csv = new Csv();
Xml xml = new Xml();
file.open(csv); // CSV opened
file.open(xml); // XML opened
}
}
class Item {
protected String type;
public void open() {
System.out.println(type + " opened"); // PO
}
}
class Csv extends Item {
public Csv() {
type = "CSV";
}
}
class Xml extends Item {
public Xml() {
type = "XML";
}
}
class File {
public void open(Item item) { // DI
item.open();
}
}
➥ Questions