Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3b7e417

Browse filesBrowse files
committed
Dynamic Programming and Polymorphism added.
1 parent 1731372 commit 3b7e417
Copy full SHA for 3b7e417

File tree

Expand file treeCollapse file tree

2 files changed

+201
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+201
-0
lines changed
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* A Naive recursive implementation of LCS problem in java*/
2+
public class LongestCommonSubsequence
3+
{
4+
5+
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
6+
int lcs( char[] X, char[] Y, int m, int n )
7+
{
8+
if (m == 0 || n == 0)
9+
return 0;
10+
if (X[m-1] == Y[n-1])
11+
return 1 + lcs(X, Y, m-1, n-1);
12+
else
13+
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
14+
}
15+
16+
/* Utility function to get max of 2 integers */
17+
int max(int a, int b)
18+
{
19+
return (a > b)? a : b;
20+
}
21+
22+
public static void main(String[] args)
23+
{
24+
LongestCommonSubsequence lcs = new LongestCommonSubsequence();
25+
String s1 = "AGGTAB";
26+
String s2 = "GXTXAYB";
27+
28+
char[] X=s1.toCharArray();
29+
char[] Y=s2.toCharArray();
30+
int m = X.length;
31+
int n = Y.length;
32+
33+
System.out.println("Length of LCS is" + " " +
34+
lcs.lcs( X, Y, m, n ) );
35+
}
36+
37+
}

‎OOPs/polymorphism.md

Copy file name to clipboard
+164Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
# Polymorphism in Java
3+
4+
- The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
5+
6+
- Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.
7+
8+
- Polymorphism is considered as one of the important features of Object Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
9+
10+
11+
### In Java polymorphism is mainly divided into two types:
12+
13+
- Compile time Polymorphism
14+
- Runtime Polymorphism
15+
16+
## Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.
17+
18+
### Method Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
19+
20+
- Example: By using different types of arguments
21+
22+
23+
// Java program for Method overloading
24+
25+
class MultiplyFun {
26+
27+
// Method with 2 parameter
28+
static int Multiply(int a, int b)
29+
{
30+
return a * b;
31+
}
32+
33+
// Method with the same name but 2 double parameter
34+
static double Multiply(double a, double b)
35+
{
36+
return a * b;
37+
}
38+
}
39+
40+
class Main {
41+
public static void main(String[] args)
42+
{
43+
44+
System.out.println(MultiplyFun.Multiply(2, 4));
45+
46+
System.out.println(MultiplyFun.Multiply(5.5, 6.3));
47+
}
48+
}
49+
50+
51+
- Example: By using different numbers of arguments
52+
53+
// Java program for Method overloading
54+
55+
class MultiplyFun {
56+
57+
// Method with 2 parameter
58+
static int Multiply(int a, int b)
59+
{
60+
return a * b;
61+
}
62+
63+
// Method with the same name but 3 parameter
64+
static int Multiply(int a, int b, int c)
65+
{
66+
return a * b * c;
67+
}
68+
}
69+
70+
class Main {
71+
public static void main(String[] args)
72+
{
73+
System.out.println(MultiplyFun.Multiply(2, 4));
74+
75+
System.out.println(MultiplyFun.Multiply(2, 7, 3));
76+
}
77+
}
78+
79+
80+
### Operator Overloading: Java also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.
81+
82+
- In java, Only “+” operator can be overloaded:
83+
84+
To add integers
85+
To concatenate strings
86+
87+
- Example:
88+
89+
90+
// Java program for Operator overloading
91+
92+
class OperatorOVERDDN {
93+
94+
void operator(String str1, String str2)
95+
{
96+
String s = str1 + str2;
97+
System.out.println("Concatinated String - "
98+
+ s);
99+
}
100+
101+
void operator(int a, int b)
102+
{
103+
int c = a + b;
104+
System.out.println("Sum = " + c);
105+
}
106+
}
107+
108+
class Main {
109+
public static void main(String[] args)
110+
{
111+
OperatorOVERDDN obj = new OperatorOVERDDN();
112+
obj.operator(2, 3);
113+
obj.operator("joe", "now");
114+
}
115+
}
116+
117+
118+
## Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
119+
120+
### Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
121+
122+
- Example:
123+
124+
125+
// Java program for Method overridding
126+
127+
class Parent {
128+
129+
void Print()
130+
{
131+
System.out.println("parent class");
132+
}
133+
}
134+
135+
class subclass1 extends Parent {
136+
137+
void Print()
138+
{
139+
System.out.println("subclass1");
140+
}
141+
}
142+
143+
class subclass2 extends Parent {
144+
145+
void Print()
146+
{
147+
System.out.println("subclass2");
148+
}
149+
}
150+
151+
class TestPolymorphism3 {
152+
public static void main(String[] args)
153+
{
154+
155+
Parent a;
156+
157+
a = new subclass1();
158+
a.Print();
159+
160+
a = new subclass2();
161+
a.Print();
162+
}
163+
}
164+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.