What is java program?
Java is a pure object oriented programming language. OOPS concepts are Class, Object, Inheritance, Polymorphism, Abstraction, and Encapsulation.
What is class?
- Class is a blueprint or prototype that defines the fields and the methods (also called functions).
- This is the template or pattern for object to be created. Every objects should follow class definition.
- class keyword is used to define the new data type. once defined, this new type can be used to create the objects of that type.
- A class is a template for an object, and an object is an instance of a class.
General structure of a class:
class classname {
type instance-variable1;
type instance-variable2;
//....
type method1(parameter1, parameter2, ...){
//defination
}
type method2(parameter1, parameter2, ...){
//defination
}
//more methods....
}
class Box{
double width;
double height;
double depth;
double calculateVolume(double width, double height, double depth);
}
What is object?
Program around its data is called object. object refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.
// Java Class Example
class Box{
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String args[]) {
//Object creation for class Box. here 'b'is the object name
Box b= new Box();
double volume;
b.width = 20;
b.height = 15;
b.depth = 10;
volume = b.width * b.height * b.depth;
System.out.println("Box Volume is " + volume);
}
}
Output:
Box Volume is 3000.0
What is inheritance in java?
- Inheritance is the process by which one object acquires the properties of another object.
- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of parent object.
- It allows to create the hierarchical classifications.
- Using inheritance, create a general class (is also called superclass or baseclass) that defines common set of related items. This class can then be inherited by other class(subclass).
- A subclass is a specialized version of superclass.
- extends keyword is used for inheriting one class into another class.
// Java inheritance example
class Parent {
int a, b;
void showab() {
System.out.println("a and b: "+ a + " " + b);
}
}
class Child extends Parent {
int c;
void showc() {
System.out.println("c: "+ c);
}
void sum() {
System.out.println("sum(a+b+c): " + (a +b+c));
}
}
class SimpleInheritance {
public static void main(String args[]) {
Parent superObj = new Parent();
Child subObj = new Child();
superObj.a = 20;
superObj.b = 30;
superObj.showab();
System.out.println();
subObj.a = 11;
subObj.b = 12;
subObj.c = 13;
subObj.showab();
subObj.showc();
System.out.println();
subObj.sum();
}
}
Output:
a and b: 20 30
a and b: 11 12
c: 13
sum(a+b+c): 36
What is polymorphism in java?
Polimorphism (from the Greek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
There are two different types of polymorphism.
- Method Overloading or Compile time polymorphism
- Method Overriding or Run time polymorphism.
What is Method Overloading?
Method Overloading is a feature that allows a class can have two or more methods having same method name if number of arguments or data type of arguments are different.
Compiler throws error if two methods having same method name and arguments(same signature).
Method overloading is done in the same class or subclass(derived class).
// Java polymorphism example
class Number
{
void show(int a)
{
System.out.println ("a: " + a);
}
void show(int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double show(double a) {
return a;
}
}
class OverloadTest
{
public static void main (String args [])
{
double result;
Number obj = new Number();
obj.show(15);
obj.show(11, 12);
result = obj.show(59.5);
System.out.println("double a: " + result);
}
}
Output:
a: 15
a and b: 11,12
double a: 59.5
What is Method Overriding?
Sub class method overrides the base class method if base class and sub class have same method name with arguments.
Applies only in inherited methods.
Object type assigned to reference variable type determines which overridden method will be used at runtime.
Static and final methods cannot be overridden.
Constructors cannot be overridden
class Parent
{
public void show(int a) //Base class method
{
System.out.println ("Parent a:" + a);
}
}
class Child extends Parent
{
public void show(int a) //Derived Class method
{
System.out.println ("Child a:" + a);
}
}
class OverrideTest
{
public static void main (String args []) {
// Parent class reference and object
Parent obj1 = new Parent();
// Parent class reference but Child class object
Child obj2 = new Child();
// Calling method from Parent class
obj1.show(15);
//Calling method from Child class
obj2.show(20);
}
}
Output:
Parent a:15
Child a:20
What is abstraction in java?
Abstraction is a process in java used to hide certain details and only show the essential features of the object.
- When designing your classes, Abstraction helps to hide internal implementation from others as far as possible.
- In OOPS, abstraction is a process of hiding the implementation details, only the functionality will be provided to the user.
- User will have the information on what the object does instead of how it does it.
- This deals with the outside view of an object.
- Abstraction is used to hide the implementation complexity.
- Abstract class and interfaces are used to implement abstractions in java code.
- Abstract keyword is used to implement abstract class or methods.
Abstract Class:
- Abstract class cannot be instantiated.
- Abstract class may or may not have abstract methods.
- Abstract method is declared or defined in a class, then class must be defined as abstract.
- Abstract class can be used in java code as inherit the abstract class and provide implementation for all abstract methods in sub class.
//abstract class
abstract class AbstractClass {
//abstract method
abstract void method1();
//We can define concrete methods still in abstract class.
void method2() {
System.out.println("This is a concrete method in abstract class");
}
}
class ChildClass extends AbstractClass {
void method1() {
System.out.println("AbstractClass method1 implementation in sub class.");
}
}
class AbstractTest {
public static void main(String args[]) {
//throws compiler error for below commented code
//AbstractClass obj = new AbstractClass();
ChildClass obj = new ChildClass();
obj.method1();
obj.method2();
}
}
Output:
AbstractClass method1 implementation in sub class.
This is a concrete method in abstract class
What is encapsulation in java?
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
- This is also known as data hiding.
- Encapsulation is as a protective wrapper(class) that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper(class).
- Access to the code and data inside wrapper is tightly controlled through a well defined interface.
- Encapsulated class can be created in java by making all the data members of the class private, and setter and getter methods can be defined as public to set and get the data in the class.
- Instance variable's of a class can be made read-only or write-only.
- A class can have entire control over instance variables what is stored.
// Java Encapsulation example
class EncapsulationClass {
private String field1;
private int field2;
private double field3;
public String getField1() {
return field1;
}
public int getField2() {
return field2;
}
public double getField3() {
return field3;
}
public void setField1(String updatedField1) {
field1 = updatedField1;
}
public void setField2(int updatedField2) {
field2 = updatedField2;
}
public void setField3(double updatedField3) {
field3 = updatedField3;
}
}
class EncapsulationTest {
public static void main(String args[]) {
EncapsulationClass obj = new EncapsulationClass();
obj.setField1("TestUser");
obj.setField2(15);
obj.setField3(100.50);
System.out.println("Field1: " + obj.getField1());
System.out.println("Field2: " + obj.getField2());
System.out.println("Field3: " + obj.getField3());
}
}
Output:
Field1: TestUser
Field2: 15
Field3: 100.5
« Previous
Next »
Java Programming Language Blog