***CONSTRUCT IN Java***
import java.io.*;
class Geek
{
int num;
String name;
Geek()
{
System.out.println("Constructor called");
}
}
class GFG
{
public static void main (String[] args)
{
Geek geek1 = new Geek();
System.out.println(geek1.name);
System.out.println(geek1.num);
}
}
output
Constructor called
null
0
*** ENCAPSULATION ***
class Encapsulate {
private String geekName;
private int geekRoll;
private int geekAge;
public int getAge() { return geekAge; }
public String getName() { return geekName; }
public int getRoll() { return geekRoll; }
public void setAge(int newAge) { geekAge = newAge; }
public void setName(String newName)
{
geekName = newName;
}
public void setRoll(int newRoll) { geekRoll = newRoll; }
}
public class TestEncapsulation {
public static void main(String[] args)
{
Encapsulate obj = new Encapsulate();
obj.setName("Harsh");
obj.setAge(19);
obj.setRoll(51);
System.out.println("Geek's name: " + obj.getName());
System.out.println("Geek's age: " + obj.getAge());
System.out.println("Geek's roll: " + obj.getRoll());
}
}
output
Geek's name: Harsh
Geek's age: 19
Geek's roll: 51
***EXCEPTION IN JAVA***
class ExceptionThrown
{
priate Exception handler is not found within this method.
static int divideByZero(int a, int b){
int i = a/b;
return i;
}
static int computeDivision(int a, int b) {
int res =0;
try
{
res = divideByZero(a,b);
}
catch(NumberFormatException ex)
{
System.out.println("NumberFormatException is occured");
}
return res;
}
public static void main(String args[]){
int a = 1;
int b = 0;
try
{
int i = computeDivision(a,b);
}
catch(ArithmeticException ex)
{
System.out.println(ex.getMessage());
}
}
}
output
/ by zero
***INHERITANCE IN JAVA***
class Bicycle{
public int gear;
public int speed;
public Bicycle(int gear, int speed)
{
this.gear = gear;
this.speed = speed;
}
public void applyBrake(int decrement)
{
speed -= decrement;
}
public void speedUp(int increment)
{
speed += increment;
}
public String toString()
{
return ("No of gears are " + gear + "\n"
+ "speed of bicycle is " + speed);
}
}
class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int gear, int speed,
int startHeight)
{
super(gear, speed);
seatHeight = startHeight;
}
public void setHeight(int newValue)
{
seatHeight = newValue;
}
@Override public String toString()
{
return (super.toString() + "\nseat height is "
+ seatHeight);
}
}
public class Test {
public static void main(String args[])
{
MountainBike mb = new MountainBike(3, 100, 25);
System.out.println(mb.toString());
}
}
output
No of gears are 3
speed of bicycle is 100
seat height is 25
***INTERFACE IN JAVA***
import java.io.*;
interface In1
{
final int a = 10;
void display();
}
class TestClass implements In1
{
public void display()
{
System.out.println("Geek");
}
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
output
Geek
10
***CONSTRUCTOR IN JAVA***
abstract class Shape {
String color;
abstract double area();
public abstract String toString();
public Shape(String color)
{
System.out.println("Shape constructor called");
this.color = color;
}
public String getColor() { return color; }
}
class Circle extends Shape {
double radius;
public Circle(String color, double radius)
{
super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}
@Override double area()
{
return Math.PI * Math.pow(radius, 2);
}
@Override public String toString()
{
return "Circle color is " + super.getColor()
+ "and area is : " + area();
}
}
class Rectangle extends Shape {
double length;
double width;
public Rectangle(String color, double length,
double width)
{
super(color);
System.out.println("Rectangle constructor called");
this.length = length;
this.width = width;
}
@Override double area() { return length * width; }
@Override public String toString()
{
return "Rectangle color is " + super.getColor()
+ "and area is : " + area();
}
}
public class Test {
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
OUTPUT
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0
***OVERLOADING IN JAVA***
public class Sum {
public int sum(int x, int y)
{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
output
30
60
31.0
***OVERRIDING IN JAVA***
class Parent {
void show()
{
System.out.println("Parent's show()");
}
}
class Child extends Parent {
@Override
void show()
{
System.out.println("Child's show()");
}
}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}
output
Parent's show()
Child's show()
***POLYORPHISM IN JAVA***
class Helper {
static int Multiply(int a, int b)
{
return a * b;
}
static double Multiply(double a, double b)
{
return a * b;
}
}
class GFG {
public static void main(String[] args)
{
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}
output
8
34.65