Course Content

Course Content

Description

Classes and Objects in Java

In our previous video of Core Java course we have seen that what are Looping Statements in Java and covered all the types of Loops in Java programming language. Now in this video we are going to see what are Classes in Java and Objects in Java.

The collection of Objects in Java programming language is called Class in Java. A class in Java programming is a blueprint or prototype from which objects in Java are created. A class in Java is a three compartment box containing the name, methods, and variables.
Syntax :
class
{
data member;
method;
}

An entity that has state and behaviour is known as an Object in Java programming language. An object in Java has three characteristics, State, Behaviour, and Identity.

State represents data(value) of an object. While Behaviour represents the behaviour for an object such as deposit, withdraw etc... And Identity is typically implemented via a unique ID.

Object in Java is an instance of a Class in Java. For example, there is one pen, so it's behaviour is to write, it's state is white in colour and like that.
Example :
class sum
{
int a=10,b=4,ans;
void add()
{
ans=a+b;
System.out.println("sum is:->"+ans);
}
public static void main(String args[])
{
sum s=new sum();
s.add();
}
}
Output :
sum is:->14

The Access modifiers in Java specifies accessibility (scope) of a data member, method, constructor or class. There are three types of Java Access modifiers :

  1. Private
  2. Protected
  3. Public

The Private Access modifier in Java is accessible only within the class.
Example :
class A
{
private int b=40;
private void show(){
System.out.println("Hello java");
}
}
public class a1
{
public static void main(String args[])
{
A aa=new A();
System.out.println(aa.data);
aa.show(),
}
}

Public is the most well known of the Java keywords. A variable or method that is public means that any class can access it. Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.

A constructor in Java is a special method that is used to initialize an object. A constructor does not have any return type. A constructor has same name as the class in which it resides. Constructor in Java can not be abstract, static, final or synchronized. Java constructor is invoked at the time of object creation.

There are two types of Constructors :
1. Default constructor (no-argument constructor)
2. Parameterized constructor (argument constructor)

A constructor that have no parameter is known as Default constructor in Java. Default constructor in Java provides the default values to the object like 0, null etc. depending on the type.
Example :
class stud
{
stud()
{
System.out.println("Default constructor");
}
public static void main(String args[])
{
stud s1=new stud();
}
}
Output :
Default constructor

A constructor in Java that have parameters is known as Parameterized constructor in Java.
Example :
class Stud1
{
int id;
String name;
Stud1(int i,String n)
{
id = i;
}
void display()
{
System.out.println(id+""+name);
}
public static void main(String args[])
{
Stud1 s1 = new Stud1(1,"akki");
Stud1 s2 = new (2,"sanju");
s1.display();
s2.display();
}
}
Output :
1 akki
2 sanju

Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.

The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.

Example :
class over
{
int id;
String name;
int age;
over()
{
System.out.println("hiiiii");
}
over(int i,String n)
{
id=i;
name=n;
}

Example :
over(int i,String nint a)
{
id = i;
name = n;
age=a;
}
void display()
{
System.out.println(id+" "+name+" "+age);
}
public static void main(String args[])
{
over s = new over();
over s1 = new over(1, "tops");
over s2 = new over(2,"angle",2);
s1.display()
s2.display();
}
}
Output :
hiiiii
1 tops 0
2 angle 2

So, this were the Classes and Objects in Java programming language. We have also seen about Access modifiers in Java and also the types of Constructors in Java. In our next video tutorial we are going to learn about the Object Oriented Programming Principles.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere