EASY_JAVA_RU Telegram 1993
🤔 Как бы выразил полиморфизм в коде?

Это способность объектов разных классов реагировать на одинаковые методы по-разному. В Java полиморфизм достигается через наследование, переопределение методов и использование абстрактных классов или интерфейсов.

🚩Пример полиморфизма в коде

Наследование и переопределение методов
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {
@Override
public void sound() {
System.out.println("Cat meows");
}
}

public class Main {
public static void main(String[] args) {
Animal myDog = new Dog(); // Полиморфизм
Animal myCat = new Cat(); // Полиморфизм

myDog.sound(); // Вывод: Dog barks
myCat.sound(); // Вывод: Cat meows
}
}


Использование интерфейсов
interface Shape {
void draw();
}

class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a Circle");
}
}

class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a Rectangle");
}
}

public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle(); // Полиморфизм
Shape shape2 = new Rectangle(); // Полиморфизм

shape1.draw(); // Вывод: Drawing a Circle
shape2.draw(); // Вывод: Drawing a Rectangle
}
}


Реальный пример использования полиморфизма
class Animal {
public void sound() {
System.out.println("Some generic animal sound");
}
}

class Dog extends Animal {
@Override
public void sound() {
System.out.println("Woof Woof");
}
}

class Cat extends Animal {
@Override
public void sound() {
System.out.println("Meow");
}
}

public class Main {
public static void main(String[] args) {
Animal[] animals = {new Dog(), new Cat(), new Animal()};

for (Animal animal : animals) {
animal.sound(); // Полиморфный вызов
}
}
}


Результат
Woof Woof
Meow
Some generic animal sound


Ставь 👍 и забирай 📚 Базу знаний
Please open Telegram to view this post
VIEW IN TELEGRAM
👍7



tgoop.com/easy_java_ru/1993
Create:
Last Update:

🤔 Как бы выразил полиморфизм в коде?

Это способность объектов разных классов реагировать на одинаковые методы по-разному. В Java полиморфизм достигается через наследование, переопределение методов и использование абстрактных классов или интерфейсов.

🚩Пример полиморфизма в коде

Наследование и переопределение методов

class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {
@Override
public void sound() {
System.out.println("Cat meows");
}
}

public class Main {
public static void main(String[] args) {
Animal myDog = new Dog(); // Полиморфизм
Animal myCat = new Cat(); // Полиморфизм

myDog.sound(); // Вывод: Dog barks
myCat.sound(); // Вывод: Cat meows
}
}


Использование интерфейсов
interface Shape {
void draw();
}

class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a Circle");
}
}

class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a Rectangle");
}
}

public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle(); // Полиморфизм
Shape shape2 = new Rectangle(); // Полиморфизм

shape1.draw(); // Вывод: Drawing a Circle
shape2.draw(); // Вывод: Drawing a Rectangle
}
}


Реальный пример использования полиморфизма
class Animal {
public void sound() {
System.out.println("Some generic animal sound");
}
}

class Dog extends Animal {
@Override
public void sound() {
System.out.println("Woof Woof");
}
}

class Cat extends Animal {
@Override
public void sound() {
System.out.println("Meow");
}
}

public class Main {
public static void main(String[] args) {
Animal[] animals = {new Dog(), new Cat(), new Animal()};

for (Animal animal : animals) {
animal.sound(); // Полиморфный вызов
}
}
}


Результат
Woof Woof
Meow
Some generic animal sound


Ставь 👍 и забирай 📚 Базу знаний

BY Java | Вопросы собесов


Share with your friend now:
tgoop.com/easy_java_ru/1993

View MORE
Open in Telegram


Telegram News

Date: |

‘Ban’ on Telegram In the “Bear Market Screaming Therapy Group” on Telegram, members are only allowed to post voice notes of themselves screaming. Anything else will result in an instant ban from the group, which currently has about 75 members. Today, we will address Telegram channels and how to use them for maximum benefit. Telegram has announced a number of measures aiming to tackle the spread of disinformation through its platform in Brazil. These features are part of an agreement between the platform and the country's authorities ahead of the elections in October. How to create a business channel on Telegram? (Tutorial)
from us


Telegram Java | Вопросы собесов
FROM American