Dr. Kamal Macwan, Assistant Professor, CSE, Nirma University
Dr. Kamal Macwan, Assistant Professor, CSE, Nirma University
Dr. Kamal Macwan, Assistant Professor, CSE, Nirma University
Kamal Macwan,
Assistant Professor,
CSE, Nirma University
Classes and objects are the two main aspects of
object-oriented programming.
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and
behavior.
The object is an instance of a class.
A class is a group of objects which have
common properties. It is a template or
blueprint from which objects are created. It is
a logical entity. It can't be physical.
<class> Banana
Fruit
Orange
Apple
We create classes and use it from another
class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the
main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
There are 3 ways to initialize object in Java.
By reference variable
By method
By constructor
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name=“Hitesh";
System.out.println(s1.id+" "+s1.name
}
}
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){System.out.println(length*width);}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Anonymous simply means nameless. An
object which has no reference is known as an
anonymous object. It can be used at the time
of object creation only.
new Student().CalculateResult(92,78,86);
A constructor initializes an object when it is created. It has the
same name as its class and is syntactically similar to a method.
However, constructors have no explicit return type.
class Game
{ int balance;
Game()
{
balance = 100;
}
}
Most often, you will need a constructor that accepts
one or more parameters. Parameters are added to a
constructor in the same way that they are added to a
method, just declare them inside the parentheses after
the constructor's name.
class Game
{ int balance;
Game(int Money)
{
balance = Money;
}
}
Game G = new Game(250);
There can be a lot of usage of java this keyword. In java,
this is a reference variable that refers to the current
object.
this can be used to refer current class instance variable.
this can be used to invoke current class method
(implicitly)
this() can be used to invoke current class constructor.
this can be passed as an argument in the method call.
this can be passed as argument in the constructor call.
this can be used to return the current class instance from
the method.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
}
class A{
void m(){System.out.println("hello m");}
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x);
}
}
class S2{
void m(S2 obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
S2 s1 = new S2();
s1.p();
}
}
class B{ class A4{
A4 obj; int data=10;
B(A4 obj){ A4(){
this.obj=obj; B b=new B(this);
} b.display();
void display(){ }
System.out.println(obj.data); public static void main(Stri
ng args[]){
//using data member of A4 class A4 a=new A4();
}
} }
}
class A{
A getA(){
return this;
}
void msg(){System.out.println("Hello java");}
}
class Test1{
public static void main(String args[]){
new A().getA().msg();
}
}
“Method overloading is a feature of Java in which
a class has more than one method of the same
name and their parameters are different.”
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Object as an argument is use to establish
communication between two or more objects
of same class as well as different class, i.e,
user can easily process data of two same or
different objects within function.
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}
Java final method
If you make any method as final, you cannot override
it.
// sub class
class Child extends Parent
{ //sub class constructor
Child()
{ System.out.println("Inside sub class");
}
}
class Main {
// main function
public static void main(String args[]) { Output :
// creating object of sub class Inside base class
Child obj=new Child(); Inside sub class
}
}
Overriding is a feature that allows a subclass or child
class to provide a specific implementation of a
method that is already provided by one of its super-
classes or parent classes. When a method in a
subclass has the same name, same parameters or
signature, and same return type(or sub-type) as a
method in its super-class, then the method in the
subclass is said to override the method in the super-
class.
Method overriding is one of the way by which java
achieve Run Time Polymorphism. The version of a
method that is executed will be determined by the object
that is used to invoke it. If an object of a parent class is
used to invoke the method, then the version in the parent
class will be executed, but if an object of the subclass is
used to invoke the method, then the version in the child
class will be executed. In other words, it is the type of the
object being referred to (not the type of the reference
variable) that determines which version of an overridden
method will be executed.
// Base Class
class Parent {
void show()
{
System.out.println("Parent's show()");
}
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override
void show()
{
System.out.println("Child's show()");
}
}
// Driver class
class Main {
public static void main(String[] args)
{
// If a Parent type reference refers Output :
Parent’s show()
// to a Parent object, then Parent's
// show is called
Parent obj1 = new Parent();
obj1.show();
Child’s show()
// If a Parent type reference refers
// to a Child object Child's show()
// is called. This is called RUN TIME
// POLYMORPHISM.
Parent obj2 = new Child();
obj2.show();
}
}
Overriding and Access-Modifiers : The access
modifier for an overriding method can allow
more, but not less, access than the
overridden method. For example, a protected
instance method in the super-class can be
made public, but not private, in the subclass.
Doing so, will generate compile-time error.
Final methods can not be overridden : If we
don’t want a method to be overridden, we
declare it as final.
Data abstraction is the process of hiding certain
details and showing only essential information
to the user.
import package.*;
import package.classname;
fully qualified name.
If you use package.* then all the classes and
interfaces of this package will be accessible but not
subpackages.