javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



Object-Oriented Programming Features

There are following concepts in Object-Oriented Programming:

  1. Class
  2. Object
  3. Abstraction
  4. Polymorphism
  5. Encapsulation
  6. Inheritance

1. Class

  • Class is a basic building block in Object Oriented Programming (OOP). Class provides the facility to put common attributes and methods in one place.
  • Class serves as a template in OOP programming. Objects are created on basis of the class. We can call that object is an instance of the class.
  • Class also gives visibility to data and methods of the class. Here, visibility means how data and methods will be used outside the class.
  • If class has private visibility, then data and methods will not be accessible to outside the class.
  • If class has public visibility, then data and methods will be accessible to outside the class.

Example

class student {
	int rollno;
	void setdata(){
		rollno = 123;
	}
	void showdata() {
		System.out.println("Roll no is"+rollno);
	}
}

Explanation of the Program

  • A class named student is created. It consists of one attribute named rollno and two methods named setdata() and showdata() .
  • Here, we can see that one class consists of both data and methods. By default, visibility of data (attributes) and methods within the class is public .
  • For more detail go to Java Class.

2. Object

  • Object is an instance of the class or we can say it is a variable of the class.
  • Object consists of variables (or data members) which are defined within the class.
  • Methods of the class are called with help of the object.
  • Many objects can be created for a class. Each object has its own value for the variables which are defined in the class.
  • Objects are different from other objects of the class because of values that they have.

Example

class student {
	int rollno;
	void setdata(){
		rollno = 123;
	}
	void showdata() {
		System.out.println("Roll no is "+rollno);
	}
}
class demostudent {
	public static void main (String args[]){
		student obj1 = new student(); // creation of object
		obj1.setdata();
		obj1.showdata();
	}
}

Output of the Program
Roll no is 123

Explanation of the Program

  • Class student consists of one data member rollno and two methods setdata() and showdata().
  • Method setdata() is used to set the value into the data member rollno.
  • Method showdata() is used to display the value of data member rollno.
  • One object named obj1 is created.
  • Both methods setdata() and showdata() are called using the object obj1.

3. Abstraction

  • Abstraction allows programmer to see the problem at overview level. Abstraction solves the complex problem in the easy way.
  • Abstraction defines the class with its member functions. It is called abstract class. Any detail of member function is not given in the abstract class.
  • Member functions are defined only with input and output parameters in the abstract class. Body of the member function is not defined in the abstract class. These member functions are called abstract functions.
  • Keyword abstract is used to define abstract class and abstract function.
  • Further class can be defined from the abstract class. These sub classes inherit members of the abstract class and provides detail of each member function.
  • No object can be created for an abstract class.

Example

abstract class student {
	abstract void coursename();
}

class cse_student extends student {
	void coursename() {
		System.out.println("Course name is Software Engineering");
	}
}

class demostudent {
	public static void main(String args[]) {
	cse_student obj1 = new cse_student();
	obj1.coursename();
  }
}

Output of the Program
Course name is Software Engineering

Explanation of the Program

  • The program consists of a class named student . Class becomes abstract by putting the keyword abstract before the keyword class .
  • Class student consist of a method named coursename() . This method becomes abstract by putting the keyword abstract .
  • One more class is defined named cse_student which is subclass of the abstract class student . In other words, class cse_student inherits features of the abstract class 'student'.
  • Subclass cse_student implements the abstract method coursename() . Note:- Any abstract method must be implemented by the subclass.
  • At last main class demostudent is created which is needed to create the object of the subclass cse_student to call the method coursename() .
  • Note:- No object can be created for the abstract class. Therefore, a sub class is created which inherits the features of abstract class and its object is created.

4. Polymorphism

  • Polymorphism provides the facility to give sane name to many functions.
  • 'Poly' means many and 'morphism' means forms . It means many forms of same thing.
  • Similarly, OOP permits to have many functions with the same name.

Example
class polydemo {
	void print(int a)
	{
		System.out.println("value of a is "+ a);
	}
	void print(int a, int b)
	{
		System.out.println("value of a is "+ a);
		System.out.println("value of b is "+ b);
	}
	void print(char c)
	{
		System.out.println("value of c is "+ c);
		
	}
}
class printdemo {
	public static void main(String args[]){
		polydemo obj1 = new polydemo();
		obj1.print(10);
		obj1.print(20,30);
		obj1.print('A');
	}
}

Output of the Program

value of a is 10
value of a is 20
value of b is 30
value of c is A

Explanation of the Program

  • Class polydemo has three functions which have same name print . It shows polymorphism.
  • Class printdemo creates an object obj1 . Object obj1 calls print functions three time with different arguments.
  • Java compiler calls specific print function on basis of either type of arguments or number of arguments. Aaguments in polymorphism functions are called signature .
  • Polymorphism functions (i.e. same name functions) are distinguished at compiler level using signature.

5. Encapsulation

  • Encapsulation is the concept that puts together data and methods in one place. This place is called class .
  • In encapsulation, data can not be accessed by outside the class. Data is only accessible through methods of the class.
  • Encapsulation basically protects access of data and/or methods from the outside world.
  • Data and methods declared within the class are called member variables and member methods of the class.

Example

class student {
	 int rollno;
	 String name;
	 
	void setdata(int r, String n) {
		 rollno = r;
		 name = n;
	 }
	void dispdata() {
		 System.out.println("Roll no is "+rollno);
		 System.out.println("Name is "+name);
	 }
}
class demo {
	
    public static void main(String args[]) {
	student obj1 = new student();
	obj1.setdata(1234, "Janhvi");
	obj1.dispdata();

  }
}

Output of the Program

Roll no is 1234
Name is Janhvi

Explanation of the Program

  • Here class student is created which consists of two data members rollno and name .
  • The class is also consists of two methods setdata() for setting values of data members rollno and name and dispdata () for displaying the values of both data members.
  • Class student shows the concept of encapsulation because class student encapsulates both data and methods.

6. Inheritance

  • Inheritance extends the features of a class. When a class is working and when it has been tested properly, then it is better to extend this class instead of making changes in the class itself.
  • Another view of using inheritance is to make further classes (inherited classes) more specialized classes. Here specialized class means that a class which has member functions which are specific (or working) to particular situations.
  • Inheritance is also used to refine features of the class or add new features to the class.
  • For more detail go to Java Inheritance.

Example

class student {
	void showdata()
	{
		System.out.println("Amit Kumar");
	}
}
class job extends student {
     void showjob ()
	 {
       System.out.println("Job is Computer Engineer");   
	 }
 }

Explanation of the Program

  • Here class job extends the class student .
  • Class job has both functions showdata() and showjob() .
  • Class job inherits showdata() function from the class student .





© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.