Java tutorial-Object-Oriented Programming

Object-Oriented Programming (OOP) in Java



Object-Oriented Programming (OOP) is central to Java language. Once we talk about OOP, We always focus on classes and objects. A class is a template that can be used to create an object. Therefore, object is the instance of the class.


Creating a class


To create a class, you must use class keyword followed by the name of the class.

Example:
public class Student
{

}

A class can have two types of members-private and public members. Private member can’t be accessed by the code outside the class. In contrast, public member can be access by the code outside the class. To declare a private member, you can use private keyword and to declare a public member, you can use public keyword. Variables declared without private or public keyword are private members.

Example:
public class Student
{
 private int stnumber;
 private String stname;
 private String stsex;
}
 

Methods


A method is a member of a class that can be called to perform a specific task. Before you can call it, you must define it in the class.

Example:
public class Student
{
  private int stnumber;
  private String stname;
  private String stsex;
  public void setnumber(int n){
     stnumber=n;
}
 public void setname(String name){
   stname=name;
}
 public void setsex(String se){
   stsex=se;
}
 public int getnumber(){
   return stnumber;
}
 public String getname(){
    return stname;
}
 
 public String getsex(){
    return stsex;
   }

}

Creating objects from a class


To access members of the class, you must create its object. The object can be created by using the new keyword.
Example:
Student stu=new Student();

Accessing a class’s public members through objects


To access public members of a class, you must write its object’s name followed by dot sign(.) and the names of its members.
Example:
class Student
{
  private int stnumber;
  private String stname;
  private String stsex;
  public void setnumber(int n){
stnumber=n;
}
public void setname(String name){
  stname=name;
}
public void setsex(String se){
   stsex=se;
}
public int getnumber(){
   return stnumber;
}
public String getname(){
   return stname;
}
public String getsex(){
  return stsex;
  }
}
public class myclass{
 public static void main(String[] args){
   Student s=new Student();//object was created.
   s.setnumber(100); //The members of class were accessed.
   s.setname("Dara");
   s.setsex("M");
   System.out.print("id="+s.getnumber()+"  name="+s.getname()+"      sex="+s.getsex());
}
}

Class Constructors


Constructor is a special method that can be used to initialize objects of the class when they are created. The constructor is declared with only the name of the class followed by brackets.
Example:
class Student
{
  private int stnumber;
  private String stname;
  private String stsex;
  Student(){
   stnumber=001;
   stname="Piseth";
   stsex="M";
}
public void setnumber(int n){
  stnumber=n;
}
public void setname(String name){
   stname=name;
}
 
public void setsex(String se){
   stsex=se;
}
public int getnumber(){
   return stnumber;
}
public String getname(){
   return stname;
}
public String getsex(){
   return stsex;
   }
 
}
public class myclass{
 public static void main(String[] args){
    Student s=new Student();
    System.out.print("id="+s.getnumber()+" name="+s.getname()+"    sex="+s.getsex());
    s.setnumber(100);
    s.setname("Dara");
    s.setsex("M");
    System.out.print("id="+s.getnumber()+" name="+s.getname()+" sex="+s.getsex());
   }
}

You also can pass parameters through constructor.
Example:
class Student
{
  private int stnumber;
  private String stname;
  private String stsex;
  Student(int n, String name, String sex){
     stnumber=n;
     stname=name;
     stsex=sex;
}
public void setnumber(int n){
    stnumber=n;
}
public void setname(String name){
    stname=name;
}
public void setsex(String se){
   stsex=se;
}
public int getnumber(){
   return stnumber;
}
public String getname(){
   return stname;
}
public String getsex(){
   return stsex;
   }
}
public class myclass{
  public static void main(String[] args){
    Student s=new Student(002, "Channa","M");
    System.out.print("id="+s.getnumber()+" name="+s.getname()+" sex="+s.getsex());
    s.setnumber(100);
    s.setname("Dara");
    s.setsex("M");
    System.out.print("id="+s.getnumber()+" name="+s.getname()+" sex="+s.getsex());
   }
}

Class inheritance


Inheritance enables a derived class or a child class to inherit properties and methods from a base class or parent class.

-Using inheritance


To illustrate the use of inheritance, now we create a base class called Animal and a derived class called Dog.
class Animal
{
   private int numberOflegs;
 
   public void setnumberOflegs(int n)
   {
      numberOflegs = n;
   }
 
   public int getnumberOflegs()
   {
      return numberOflegs;
   }
}

Now we will create the Dog class. The extends keyword is used to inherit from the Animal.
class Dog extends Animal
{
   private String dogName;
 
   public void setdogName(String name)
   {
      dogName = name;
   }
 
   public String getdogName()
   {
      return dogName;
   }
}

Now we create a TestInteritance to run the program.
public class TestInheritance
{
   public static void main(String[] args)
   {
      Dog d= new Dog();
      d.setnumberOflegs(4);//setnumberOflegs was inherited from the Animal class
      d.setdogName("Aluk");
      System.out.println("Dog Name: " + d.getdogName());
      System.out.println("Number of legs: " + d.getnumberOflegs());
//getnumberOflegs was inherited from the Animal class
   }
}

Abstract Class


An abstract class is created for the purpose of being inherited. We cannot create objects from the abstract class.
abstract class Cls
{
   public int sum(int a,int b){return 0;};
}

//implementiing the abstract class

public class Calc extends Cls{

  public static void main(String ars[]){

    Calc cal=new Calc();

    int result=cal.sum(10,20);

   System.out.println("Result:"+result);

   }

//overriding the sum() method

  public int sum(int a,int b){

    return(a+b);

  }

}


Interfaces


An interface can be used as a template for other classes. Its purpose is to be inherited. It sounds like an abstract class. However, in the interface, you cannot declare variables unless they are constants. In addition, methods can be declared, but those are without bodies. You can implement the interface by using "implements" keyword.
Example:
interface Inter
{
 public void print();
}

public class Print implements Inter{
  public static void main(String args[]){
  Print bs=new Print();
  bs.print();
}

public void print(){
   System.out.println("Implementing interface example");
}

}



  • Why and How to learn
  • C programming language?
  • C++ programming language?
  • C# programming language?
  • Java programming language?
  • Python programming language?
  • VB programming language?
comment

Posted comments

Phan Neth, CICI, year 3:

Now it is ok teacher. I can connect to database and select data from it. Than you so much teacher !!!!!

04-30-2013

Dara:

Copy and paste the link below to the address box to learn how to connect C and Mysql server:
http://www.worldbestlearningcenter.com/index_files/cpp-tutorial-connect_mysql_database.htm

04-27-2013

Phan Neth, CICI, year 3:

Hello teacher i want to connect C programming language to MySql server but it have problem. When i compile it always error with library " my_global.h
and mysql.h", so how should i do?

04-27-2013

Phan Neth, Year 3 at CICI,:

I have some problem about connection to mysql server. So I hope teacher can help me. Thank !!!!

04-26-2013

Federico:

Output appears the same also this way:

static void Main(string[] args)
{
int[,] myArray = new int[5, 5];
int row, i;
int n = 1;

for (row = 0; row < 5; row++)
{
for (i = 0; i < 5; i++)
{
myArray[row, i] = n;
if (row == 1 && i == 2) Console.Write(" \t");
else Console.Write("{0} \t", n);

if (n!=26) n++;
if((i+1)%5==0) Console.WriteLine("\n");
}
}
Console.ReadLine();
}

04-13-2013

Dara:

Let pointer p point to the index 1 elem (value=2) of the array.
int *p= a+1;
Let pointer q point to the index 6 elem (value=5) of the array.
int *q= a+6;
-Expression q-p is the subtraction of address of the index 6 element and the address of index 1 elem of the array. So, the result is 5.
-Expression *p+*q is the sum of the value of index 1 elem (2) and the value of index 6 elem (5). So, the result is 7.
In conclusion, the output is 57.

03-28-2013

Anushka :

Find the output of following code
main()
{
int a[] = {1,2,9,8,6,3,5,7,8,9};
int *p= a+1; int *q= a+6;
cout<< q-p <<*p+*q; }

03-28-2013

Dara:

Option Explicit
Dim Cn as New ADODB.Connection
Private Sub_Form_Load()
Cn.Open "Provider=SQLOLEDB; Data Source=ServerName; Initial Catalog=DatabaseName;UserID=UserName; Password=Passw"
End Sub
You will need to replace ServerName with the name of your server (installed SQL Server), DatabaseName withe the name of your data file that you want to connect to, UserName with the name of the user to login to SQL Server, and Passw with the password used to login to SQL Server.

03-13-2013

More>>>

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Computer-Wbest
Programming Tips
Download
Related Posts