Introduction to Object Oriented Programming in Java

1.Class- Class is the blue print or boiler plate of our project. It consists of attributes(Colour, names and model) and non static methods(functions like turnOn( ); etc) of objects.

Microphone Class

//Microphone class
public class CreatingClass {
//Atrributes
String name="sony";
String colour="Blue";
int model=123;
//Methods
public void turnOn(){
System.out.println("Turned on");
}
public void adjustVol(){
System.out.println("Volume adjusted");
}
public void turnoff(){
System.out.println("Turned off");
}
public void description(){
System.out.println(this.name+" is my brand. "+"My colour is "+this.colour);
}
}

Main class

public class CreatingClassMain {
public static void main(String[] args) {
CreatingClass Microphone1=new CreatingClass();
Microphone1.turnoff();
System.out.println(Microphone1.name);
Microphone1.description();

}

}

Task execution finished

Turned off

sony

sony is my brand. My colour is Blue

2.Creating multiple objects of same kind having different Attributes.

Microphone class

public class CreatingSecondClass {
//Attributes
String name;
String colour;
String model;
//Methods
public void turnOn(){
System.out.println(this.name+" is Turned on.");
}
public void adjustVol(){
System.out.println(this.name+" Volume adjusted.");
}
public void turnOff(){
System.out.println(this.name+" is turned Off.");
}
}

Microphone1 & Microphone2 are same kind of objects with different attributes.

Main class

    public static void main(String[] args) {
CreatingSecondClass Microphone1= new CreatingSecondClass();
Microphone1.name="SONY";
Microphone1.colour="Red";
Microphone1.model="123";

CreatingSecondClass Microphone2= new CreatingSecondClass();
Microphone2.name="Blue Yeti";
Microphone2.colour="Blue";
Microphone2.model="456";

System.out.println(Microphone1.name);
Microphone2.turnOn();
Microphone2.adjustVol();
Microphone2.turnOff();

}
}

Task execution finished

SONY

Blue Yeti is Turned on.

Blue Yeti Volume adjusted.

Blue Yeti is turned Off.


3.constructor- 

Suppose we want to design 5 Microphones with different attributes. If we go on assigning its name, colour, model, etc in the main class(like we did in 2),  then it will look ugly and the process is also time consuming.

Constructor helps us to write less code by adding attributes in the parameters corresponding to the objects in the main class.

Microphone class

public class CreatingConstructor {
//Attributes
String name;
String colour;
int model;

//constructor
CreatingConstructor(String name, String colour, int model ){
this.name=name;
this.colour=colour;
this.model=model;
}

//Methods

public void description(){
System.out.println("My name is "+name+". I am painted with "+colour+".");

}

}

Microphone1, Microphone2, Microphone3, Microphone4 & Microphone5 are objects. Corresponding to these objects we have unique attributes assigned in the parameters.

Main Class

public class CreatingConstructorMain {
public static void main(String[] args) {
CreatingConstructor Microphone1 = new CreatingConstructor("sony", "red", 123);
CreatingConstructor Microphone2 = new CreatingConstructor("Redmi", "blue", 456);
CreatingConstructor Microphone3 = new CreatingConstructor("Nokia", "yelow", 789);
CreatingConstructor Microphone4 = new CreatingConstructor("corona", "greeen", 101);
CreatingConstructor Microphone5 = new CreatingConstructor("Anjana", "violet", 112);

System.out.println(Microphone3.name);
Microphone2.description();
}
}

Task execution finished

Nokia

My name is Redmi. I am painted with blue.

4. Inheritance- The idea behind inheritance in Java is that we can create new classes that are built upon existing classes.

Suppose we have Vehicle class. Now we can build Car & Bicycle Class upon the existing vehicle class.(Vehicle- Super Class, Car- child class, Bicycle- child class)

Vehicle class

public class StudingInheritanceWithVehicle {
//attributes
String name;
String model;
int wheel;

//constructor
StudingInheritanceWithVehicle(String name, String model, int wheel){
this.name=name;
this.model=model;
this.wheel=wheel;
}
//methods
public void start(){
System.out.println(this.name+ " is started");
}
public void brake(){
System.out.println(this.name+ " brake is applied");
}
}

Car class

public class StudyingInharanceChildCar extends StudingInheritanceWithVehicle {
int windows;
StudyingInharanceChildCar(String name, String model, int wheel, int windows) {
super(name, model, wheel);//Adding attributes directly from Vehicle class
this.windows=windows;
}
}

Bicycle class

public class StudingInharanceChildBicycle extends StudingInheritanceWithVehicle {
int pedal;
StudingInharanceChildBicycle(String name, String model, int wheel, int pedal){
super(name,model,wheel);//Adding attributes directly from Vehicle class
this.pedal=pedal;
}

}

Main Class

public class StudingInheritanceMain {
public static void main(String[] args) {
StudyingInharanceChildCar obj1= new StudyingInharanceChildCar("Alto","ABC",4,4);
StudingInharanceChildBicycle obj2=new StudingInharanceChildBicycle("Herculis","xyz",2,2);

obj1.start();
System.out.println(obj1.model);
obj2.brake();
System.out.println(obj2.pedal);

}
}

Task execution finished

Alto is started

ABC

Herculis brake is applied

2

5.Gatters & Setters- Due to security purpose some times we need to make our attributes private. Getters are used to retrieve private attributes. Setters are used to modify private attributes.

Set&Get Class

public class UnderstandingSettersGetters {

//attributes

private String name;
private String colour;
private String loud;
private int model;
//constructor
public UnderstandingSettersGetters(String name,String colour,int model){
this.name=name;
this.colour=colour;
this.model=model;
}
//----------------------------------------------
//overloading constructor
public UnderstandingSettersGetters(String name,String loud){
this.name=name;
this.loud=loud;
}
//----------------------------------------------

//get&set

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColour(){
return colour;
}
public void setColour(String colour){
this.colour=colour;
}
public int getModel(){
return model;
}
public void setModel(int model){
this.model=model;
}
//--------------------------------------
//overloading constructor getter and setter

public String getLoud(){
return loud;
}
public void setLoud(String loud){
this.loud=loud;
}
//----------------------------------------


//methods

public void description(){
System.out.println("My name is "+this.name+". I am painted "+this.colour+".");
}

}

Main Class

public class UnderstandingSettersGettersMain {
public static void main(String[] args) {
UnderstandingSettersGetters obj1=new UnderstandingSettersGetters("Blue yeti","blue",123);
obj1.setName("Red Yeti");//changing name
//-------------------------------------------
UnderstandingSettersGetters obj2=new UnderstandingSettersGetters("Blue yeti","heavy");
obj2.setName("Green Mic");
obj2.setLoud("light");

System.out.println(obj2.getName());
System.out.println(obj2.getLoud());
obj2.description();
//---------------------------------------------



System.out.println(obj1.getColour());
System.out.println(obj1.getName());

obj1.description();


}


}

Task execution finished

Green Mic

light

My name is Green Mic. I am painted null.

blue

Red Yeti

My name is Red Yeti. I am painted blue.


#Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task.

To be Continued....

Comments

Post a Comment

Popular posts from this blog

Java Array Problems