Part 8: Java Polymorphism with its real time example code.

JAVA

Java Polymorphism

Polymorphism means “many forms”, It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class. It occurs when we have many classes that are related to each other by inheritance. Like we definite in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in diverse ways.

For example, think of a superclass called Tree that has a method called treeSound(). Subclasses of Tree could be SmallTree, MidSizeTree.  And they also have their own implementation of an tree sound:

Example
class Tree {
public void treeSound() {
System.out.println(“The tree makes a sound in wind”);
}
}
class SmallTree extends Tree {
public void treeSound() {
System.out.println(“The SmallTree can also makes a sound in wind”);
}
}
class MidSizeTree extends c {
public void treeSound() {
System.out.println(“The MidSizeTree can also makes a sound in wind”);
}
}

Remember from the Inheritance chapter that we use the extends keyword to inherit from a class.Now we can create Pig and Dog objects and call the animalSound() method on both of them:

Example
class Tree {
public void treeSound() {
System.out.println(“The tree makes a sound in wind”);
}
}
class SmallTree extends Tree {
public void treeSound() {
System.out.println(“The SmallTree can also makes a sound in wind”);
}
}

class MidSizeTree extends c {
public void treeSound() {
System.out.println(“The MidSizeTree can also makes a sound in wind”);
}
}
class Main {
public static void main(String[] args) {
Tree myTree = new Tree(); // create Tree obj
Tree mySmall = new SmallTree();// create SmallTree obj
Tree myMidSize = new MidSizeTree();// create MidSizeTree obj
myTree.treeSound();
mySmall.treeSound();
mymyMidSize.treeSound();
}
}

OUTPUT

The tree makes a sound in wind
The SmallTree makes a sound in wind
The MidSizeTree can also makes a sound in wind

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...

Database basic overview

What is DBMS? A Database Management System (DBMS) is a collection of interrelated data and a set of programs to access those data. Database management systems (DBMS) are computer software applications that interact with the user, other applications, and the database itself to capture and analyze data. Purpose of Database Systems The collection of data, usually...

Laravel – Scopes (3 Easy Steps)

Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. In simple terms laravel scope is just a query, a query to make the code shorter and faster. We can create custom query with relation or anything with scopes. In any admin project we need to get data...