1.
write a program to demonstrate class and object
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Output: 5
2. w.p.d method overloading
3.
class Adder{
4.
static int add(int a,int b){return a+b;}
5.
static int add(int a,int b,int c){return a+b+c;}
6.
}
7.
class TestOverloading1{
8.
public static void main(String[] args){
9.
System.out.println(Adder.add(11,11));
10. System.out.println(Adder.add(11,11,11));
11. }}
Output:
22
33
3. constructor overloading
public class Tester {
private String message;
public Tester(){
message = "Hello World!";
}
public Tester(String message){
this.message = message;
}
public String getMessage(){
return message ;
}
public void setMessage(String message){
this.message = message;
}
public static void main(String[] args) {
Tester tester = new Tester();
System.out.println(tester.getMessage());
Tester tester1 = new Tester("Welcome");
System.out.println(tester1.getMessage());
}
}
Output: helloworld
Welcome
4. order of initialization
class GFG {
GFG(int x)
{
System.out.println("ONE
argument constructor");
}
GFG()
{
System.out.println("No
argument constructor");
}
static
{
System.out.println("1st
static init");
}
{
System.out.println("1st
instance init");
}
{
System.out.println("2nd
instance init");
}
static
{
System.out.println("2nd
static init");
}
public static void main(String[]
args)
{
new GFG();
new GFG(8);
}
}
Output:
1st static init2nd static init1st instance init2nd instance initNo argument constructor1st instance init2nd instance initONE argument constructor
5. This object
public class Main {
int x;
// Constructor with a parameter
public Main(int x) {
this.x = x;
}
// Call the constructor
public static void main(String[] args) {
Main myObj = new Main(5);
System.out.println("Value of x = " + myObj.x);
}
}
Output:
Value of X=5;
6. single inheritance
1.
class Animal{
2.
void eat(){System.out.println("eating...");}
3.
}
4.
class Dog extends Animal{
5.
void bark(){System.out.println("barking...");}
6.
}
7.
class TestInheritance{
8.
public static void main(String args[]){
9.
Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }}
Output:
Barking…
Eating…
7. multiple inheritance
1.
class Animal{
2.
void eat(){System.out.println("eating...");}
3.
}
4.
class Dog extends Animal{
5.
void bark(){System.out.println("barking...");}
6.
}
7.
class BabyDog extends Dog{
8.
void weep(){System.out.println("weeping...");}
9.
}
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16. }}
Output:
Weeping…
Barking
Eating…
8. abstract class
1.
abstract class Bike{
2.
abstract void run();
3.
}
4.
class Honda4 extends Bike{
5.
void run(){System.out.println("running safely");}
6.
public static void main(String args[]){
7.
Bike obj = new Honda4();
8.
obj.run();
9.
}
10. }
Output:
Running safely
9. multiple inheritance using interfaces
interface AnimalEat {
void eat();
}
interface AnimalTravel {
void travel();
}
class Animal implements AnimalEat, AnimalTravel {
public void eat() {
System.out.println("Animal is eating");
}
public void travel() {
System.out.println("Animal is travelling");
}
}
public class Demo {
public static void main(String args[]) {
Animal a = new Animal();
a.eat();
a.travel();
}
}
Output:
Animal is eating
Animal is travelling
10 runtime polymorphism
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Output:
Animals can move
Animals can walk and run
11. Reverse polymorphism
12. public class ReverseNumberExample1
13. {
14. public static void main(String[] args)
15. {
16. int number = 987654, reverse = 0;
17. while(number != 0)
18. {
19. int remainder = number % 10;
20. reverse = reverse * 10 + remainder;
21. number = number/10;
22. }
23. System.out.println("The reverse of the given number is: " + reverse);
24. }
25. }
Output:
The reverse of the given number is: 456789
13. Overriding to string method
14.
class Complex
{
15.
private double re,
im;
16.
17.
public Complex(double re,
double im) {
18.
this.re
= re;
19.
this.im
= im;
20.
}
21.
}
22.
23.
// Driver class to test the Complex class
24.
public class Main
{
25.
public static void main(String[]
args) {
26.
Complex
c1 = new Complex(10, 15);
27.
System.out.println(c1);
28.
}
29.
}
Output:
Complex@214c265e
12. Protecting variable
13. package com.java;
14.
15. public class A {
16.
17. protected String msg="Try to access the protected variable outside the package";
18.
19. }
20. //save by ProtectedExample1.java
21. package com.javatpoint;
22. import com.java.A;
23.
24. public class ProtectedExample1 {
25. public static void main(String[] args) {
26. A a=new A();
27. System.out.println(a.msg);
28.
29. }
30. }
Output:
Try to access the protected variable outside the package
14. packages
1.
//save by A.java
2.
package pack;
3.
public class A{
4.
public void msg(){System.out.println("Hello");}
5.
}
1.
//save by B.java
2.
package mypack;
3.
import pack.*;
4.
5.
class B{
6.
public static void main(String args[]){
7.
A obj = new A();
8.
obj.msg();
9.
}
10. }
Output:hello
15. arrays
class GFG {
public
static void main(String[]
args)
{
// declares
an Array of integers.
int[] arr;
//
allocating memory for 5 integers.
arr = new int[5];
//
initialize the first elements of the array
arr[0] = 10;
//
initialize the second elements of the array
arr[1] = 20;
// so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing
the elements of the specified array
for (int i =
0; i < arr.length; i++)
System.out.println("Element
at index " + i
+
" : " + arr[i]);
}
}
Output:
Element at index 0 : 10Element at index 1 : 20Element at index 2 : 30Element at index 3 : 40Element at index 4 : 50
16: wrapper classes
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
for (int i : myNumbers) {
System.out.println(i);
}
}
}
Output:
10
15
20
25
17: string functions
1.
public class StringExample{
2.
public static void main(String args[]){
3.
String s1="java";//creating string by Java string literal
4.
char ch[]={'s','t','r','i','n','g','s'};
5.
String s2=new String(ch);//converting char array to string
6.
String s3=new String("example");//creating Java string by new keyword
7.
System.out.println(s1);
8.
System.out.println(s2);
9.
System.out.println(s3);
10. }}
Output:
Java
String
Functions
18: string buffer
public class StringBufferExample
{
public static void main(String[]
args) {
StringBuffer
sb = new StringBuffer();
sb.append("Hello");
sb.append("
");
sb.append("world");
String
message = sb.toString();
System.out.println(message);
}
}
Output:
Helloworld
19: exception handling
1.
public class JavaExceptionExample{
2.
public static void main(String args[]){
3.
try{
4.
//code that may raise exception
5.
int data=100/0;
6.
}catch(ArithmeticException e){System.out.println(e);}
7.
//rest code of the program
8.
System.out.println("rest of the code...");
9.
}
10. }
Output:
Exception in the thread main java.lang.arithmaticexception:/by zero
20: fileinput and outputstreams
import java.io.*;
class Demo_print {
public static void main(String[]
args)
{
// using
print()
// all
are printed in the
// same
line
System.out.print("GfG!
");
System.out.print("GfG!
");
System.out.print("GfG!
");
}
}
Output:
GFG! GFG! GFG!
21: overriding run method in a thread class
public class ThreadTest {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
class MyThread extends Thread {
@Override
public void start() { // overriding the start() method
System.out.println("Overriding a start() method");
super.start();
}
@Override
public void run() {
System.out.println("run() method ");
}
}
Output:
Overriding a start() methodrun() method
22: implementing runnable interface
public class RunnableDemo {
public static void main(String[]
args)
{
System.out.println("Main
thread is- "
+
Thread.currentThread().getName());
Thread t1
= new Thread(new RunnableDemo().new RunnableImpl());
t1.start();
}
private class RunnableImpl
implements Runnable {
public void run()
{
System.out.println(Thread.currentThread().getName()
+
", executing run() method!");
}
}
}
Output:
Main thread is- mainThread-0, executing run() method!
23: vector class
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[]
args)
{
// Size
of the Vector
int n
= 5;
//
Declaring the Vector with
//
initial size n
Vector<Integer>
v = new Vector<Integer>(n);
//
Appending new elements at
// the
end of the vector
for (int i
= 1; i <= n; i++)
v.add(i);
//
Printing elements
System.out.println(v);
// Remove
element at index 3
v.remove(3);
//
Displaying the vector
// after
deletion
System.out.println(v);
//
iterating over vector elements
// using
for loop
for (int i
= 0; i < v.size(); i++)
//
Printing elements one by one
System.out.print(v.get(i)
+ " ");
}
}
Output:
[1, 2, 3, 4, 5][1, 2, 3, 5]1 2 3 5
24: generic class
public class Area<T>
{
// T is the Datatype like String,
// Integer of which Parameter
type,
// the class Area is of
private T
t;
public void add(T
t)
{
// this.t
specify the t variable inside
// the
Area Class whereas the right hand
// side t
simply specify the value as the
//
parameter of the function add()
this.t =
t;
}
public T
get() { return t; }
public void getArea()
{}
public static void main(String[]
args)
{
// Object
of generic class Area with parameter Type
// as
Integer
Area<Integer>
rectangle = new Area<Integer>();
// Object
of generic class Area with parameter Type
// as
Double
Area<Double>
circle = new Area<Double>();
rectangle.add(10);
circle.add(2.5);
System.out.println(rectangle.get());
System.out.println(circle.get());
}
}
Output:
10
2.5
25: applet
// This is a Simple Java Applet
// program using appletviewer
import java.applet.*;
import java.awt.*;
/*
<applet code="AppletExp1" width=600 height=300>
</applet>
*/
public class AppletExp1 extends Applet {
public void init()
{
System.out.println("Initializing an applet");
}
public void start()
{
System.out.println("Starting an applet");
}
public void stop()
{
System.out.println("Stopping an applet");
}
public void destroy()
{
System.out.println("Destroying an applet");
}
}
Output:

Such a helpful and educational essay this is. I finally understand this topic now, thanks to your straightforward writing.
ReplyDeleteAngular Frontend Development Company Chennai