Skip to content

Commit 22b6a39

Browse files
authoredAug 9, 2017
Add files via upload
1 parent 0269e04 commit 22b6a39

37 files changed

+1014
-0
lines changed
 

‎Constructor/Car.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Constructor;
2+
3+
class CarCreat {
4+
5+
int regNum;
6+
private static int num=1000;
7+
8+
CarCreat(){regNum=num++;}
9+
10+
void cardetails(){
11+
System.out.println("Your car: "+regNum);
12+
}
13+
14+
void start(){
15+
System.out.println("Car Starts. ");
16+
}
17+
void accelerate(){
18+
System.out.println("Car Accelerate. ");
19+
}
20+
void stop(){
21+
System.out.println("Car Stops. ");
22+
}
23+
}
24+
25+
class Driver{
26+
String name;
27+
CarCreat c=new CarCreat();
28+
Driver(String n){
29+
name=n;
30+
}
31+
void driverName(){
32+
System.out.println(name);
33+
}
34+
}
35+
36+
public class Car{
37+
public static void main(String... args){
38+
39+
Driver dr=new Driver("xyz");
40+
dr.driverName();
41+
dr.c.cardetails();
42+
dr.c.start();
43+
44+
Driver dr1=new Driver("abc");
45+
dr.driverName();
46+
dr1.c.cardetails();
47+
dr1.c.start();
48+
}
49+
}

‎Constructor/ConstEx1.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Constructor;
2+
3+
public class ConstEx1 {
4+
5+
// if we won't provide any constructor compiler adds default constructor
6+
// int this case, ConstEx1(){} , but if we provide any other constructor
7+
// compiler won't add.
8+
9+
int x, y;
10+
11+
// no-argument constructor
12+
ConstEx1() {
13+
getX(); // we can call both static and non-static method from
14+
// constructor
15+
x = 0;
16+
y = 0;
17+
}
18+
19+
// Parameterized Constructor
20+
ConstEx1(int l) {
21+
22+
x = y = l;
23+
getX();
24+
}
25+
26+
ConstEx1(int l, int m) {
27+
x = l;
28+
y = m;
29+
}
30+
31+
static void getX() {
32+
System.out.println(" You're inside method getX() ");
33+
// return x;
34+
}
35+
36+
public static void main(String[] args) {
37+
ConstEx1 c1 = new ConstEx1();
38+
System.out.println("[C1]X= " + c1.x + " Y= " + c1.y);
39+
40+
ConstEx1 c2 = new ConstEx1(20);
41+
ConstEx1 c3 = new ConstEx1(12, 13);
42+
43+
System.out.println("[C2]X= " + c2.x + " Y= " + c2.y);
44+
System.out.println("[C3]X= " + c3.x + " Y= " + c3.y);
45+
}
46+
}

‎Constructor/CopyConst.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Constructor;
2+
3+
public class CopyConst {
4+
5+
double real, imaginary;
6+
7+
public CopyConst(double re, double img) {
8+
real = re;
9+
imaginary = img;
10+
}
11+
12+
// Copy Constructor
13+
public CopyConst(CopyConst c) {
14+
15+
real = c.real;
16+
imaginary = c.imaginary;
17+
}
18+
19+
@Override // toString() is the override method of Object Class
20+
public String toString() {
21+
return "(" + real + " + " + imaginary + "i)";
22+
}
23+
24+
public static void main(String[] args) {
25+
26+
CopyConst c1 = new CopyConst(10, 15);
27+
CopyConst c2 = new CopyConst(c1);
28+
CopyConst c3 = c1;
29+
30+
System.out.println("[C2]" + c2); // if we directly print object Output
31+
// Constructor.CopyConst@6d06d69c
32+
33+
System.out.println("[C3]" + c3); // on directly printing object it will
34+
// call toString() of Object class
35+
}
36+
37+
}

‎Inheritance/InheritEx.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Inheritance;
2+
3+
class SuperClass {
4+
int i, j;
5+
6+
void showij() {
7+
System.out.println("i and j: " + i + " " + j);
8+
}
9+
}
10+
11+
class SubClass extends SuperClass {
12+
int k;
13+
14+
void showk() {
15+
System.out.println("k:" + k);
16+
}
17+
18+
void sum() {
19+
System.out.println(i + j + k);
20+
}
21+
22+
}
23+
24+
class InheritEx {
25+
public static void main(String args[]) {
26+
SuperClass sup = new SuperClass();
27+
SubClass sub = new SubClass();
28+
29+
sup.i = 10;
30+
sup.j = 20;
31+
System.out.println("Contents of super class ");
32+
sup.showij();
33+
sub.i = 7;
34+
sub.j = 8;
35+
sub.k = 9;
36+
System.out.println("Contents of sub class");
37+
sub.showij();
38+
sub.showk();
39+
System.out.println("Sum of i, j and k in sub class");
40+
sub.sum();
41+
}
42+
}

‎Inheritance/NblockInherit.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Inheritance;
2+
3+
class Box1 {
4+
{
5+
System.out.println(1);
6+
}
7+
}
8+
9+
class Box2 extends Box1 {
10+
{
11+
System.out.println(2);
12+
}
13+
}
14+
15+
class Box3 extends Box2 {
16+
{
17+
System.out.println(3);
18+
}
19+
}
20+
21+
public class NblockInherit {
22+
public static void main(String[] args) {
23+
Box3 c = new Box3();
24+
System.out.println(c);
25+
}
26+
}

‎Inheritance/SblockInherit.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Inheritance;
2+
3+
class Box11 {
4+
static {
5+
System.out.println(1);
6+
}
7+
}
8+
9+
class Box12 extends Box11 {
10+
static {
11+
System.out.println(2);
12+
}
13+
}
14+
15+
class Box13 extends Box12 {
16+
static {
17+
System.out.println(3);
18+
}
19+
}
20+
21+
public class SblockInherit {
22+
public static void main(String[] args) {
23+
Box13 c = new Box13();
24+
System.out.println(c);
25+
}
26+
}

‎Inheritance/SingleLevel.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Inheritance;
2+
3+
class A {
4+
private int i = 12;
5+
protected int j = 13;
6+
public int k = 14;
7+
int m = 3;
8+
9+
A() {
10+
System.out.println("I'm Constructor from class A.");
11+
}
12+
13+
private void m1() {
14+
System.out.println("Private method of class A " + i);
15+
}
16+
17+
protected void m2() {
18+
System.out.println("Protected method of class A " + j);
19+
m1();
20+
}
21+
22+
public void m3() {
23+
System.out.println("Public method of class A " + k);
24+
}
25+
26+
void m4() {
27+
System.out.println("Package method of class A" + i + " " + m);
28+
}
29+
}
30+
31+
class B extends A {
32+
33+
B() {
34+
System.out.println("I'm Constructor from class B.");
35+
}
36+
37+
public void m5() {
38+
// System.out.println(i); // private variables not accessible in
39+
// sub-class
40+
System.out.println(" Method from class B. ");
41+
System.out.println("Protected j= " + j);
42+
System.out.println("public k= " + k);
43+
System.out.println("Package m= " + m);
44+
// m1(); // Private method is not accessible in sub-class
45+
m2();
46+
m3();
47+
m4();
48+
}
49+
50+
}
51+
52+
public class SingleLevel {
53+
public static void main(String[] args) {
54+
B b = new B();
55+
System.out.println("----------------------------------");
56+
System.out.println(" From Main method. ");
57+
b.m5();
58+
b.m4();
59+
System.out.println("----------------------------------");
60+
}
61+
62+
}

‎Inheritance/TrickyEx.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Inheritance;
2+
3+
class Test {
4+
static int i = 56;
5+
6+
static {
7+
i = i-- - --i;
8+
System.out.println("i value Static Block of Test Class: "+i);
9+
}
10+
11+
{
12+
i = i++ + ++i;
13+
System.out.println("i value Non-Static Block of Test Class: "+i);
14+
}
15+
}
16+
17+
class Test1 extends Test {
18+
static {
19+
i = --i - i--;
20+
System.out.println("i value Static Block of Test1 Class: "+i);
21+
}
22+
23+
{
24+
i = ++i + i++;
25+
System.out.println("i value Non-Static Block of Test1 Class: "+i);
26+
}
27+
}
28+
29+
public class TrickyEx {
30+
public static void main(String[] args) {
31+
// Test1 b = new Test1();
32+
// System.out.println(b.i);
33+
System.out.println(Test.i);
34+
}
35+
}

‎MethodOverloading/NullTest1.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package MethodOverloading;
2+
3+
public class NullTest1 {
4+
5+
/*
6+
* If more than one member method is both accessible and applicable to a method invocation,
7+
* it is necessary to choose one to provide the descriptor for the run-time method dispatch.
8+
*
9+
* The Java programming language uses the rule that the most specific method is chosen.
10+
* */
11+
12+
public static void method(Object obj){
13+
System.out.println("method with param type - Object");
14+
}
15+
16+
public static void method(String obj){
17+
System.out.println("method with param type - String");
18+
}
19+
20+
// public static void method(StringBuffer strBuf){
21+
// System.out.println("method with param type - StringBuffer");
22+
// }
23+
24+
/*
25+
* Why is the compiler not able to pick 'the most specific' here - because both "String" and "StringBuffer" are are sub-classes.
26+
of the Object class, but without being in the same inheritance hierarchy. For finding
27+
'the most specific' method, the compiler needs to find a method having the parameter type,
28+
which is a sub-class of the parameter types of all other overloaded methods.
29+
* */
30+
31+
public static void main(String [] args){
32+
method(null);
33+
}
34+
}
35+

‎MethodOverloading/NullTest2.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package MethodOverloading;
2+
3+
public class NullTest2 {
4+
5+
public static void method(Object obj, Object obj1){
6+
System.out.println("method with param types - Object, Object");
7+
}
8+
9+
public static void method(String str, Object obj){
10+
System.out.println("method with param types - String, Object");
11+
}
12+
/*
13+
* In this case the compiler can easily pick 'the most specific' as the method having parameter types (String, Object) as
14+
the other overloaded method is having its parameter types as (Object, Object) - clearly 'String' is a subclass of 'Object'
15+
and the other parameter is of same type, so the method with parameter types (String, Object) can be picked with ease.
16+
*
17+
* */
18+
public static void main(String [] args){
19+
method(null, null);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.