2. Class and objects
class is a template for an object
an object is an instance of a class
Creating class:
class classname {
type instance-variable1;
Type instance-variable2; // ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
} // ...
type methodnameN(parameter-list) {
// body of method
}
}
Creating object :
class-var = new classname ( );
Continued…
3. //creating class
class s2 {
int a=10;
void num(){
System.out.println(a);
}
public static void main(String agrs[]){
s2 object = new s2(); //creating object
object.num();
}
}
Class and objects
4. Abstration
Abstraction in Java is the process of hiding the implementation details and
only showing the essential functionality or features to the user.
Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details.
In Java, abstraction is achieved by interfaces and abstract classes.
Using abstract class will only help us to acheive partially abstraction.
We can achieve 100% abstraction using interfaces.
(will study later with coding example)
5. Polymorphism
one name many forms. We can achieve polymorphism by changing the no. of parameters of method or
by changing the datatype of method
Types of Java Polymorphism
Compile-Time Polymorphism (method overloading)
Runtime Polymorphism (method overriding)
6. Compile-time polymorphism (method overloading)
public class Main{
void add(){ //method overloading
int a=30,b=20;
System.out.println((a+b));
}
void add(int a , int b){//method overloading
System.out.println((a+b));
}
public static void main(String[] args) {
Main ob= new Main();
ob.add();
ob.add(10,20);
}}
7. Run-time polymorphism (method overriding)
class mca_1st_year {
void add(){
System.out.println("super class");
}
}
public class a extends mca_1st_year{ //inheritence
void add(){
//super.add(); // for calling parent class
method
System.out.println("sub class");
}
public static void main(String args[]){
a obj = new a();
obj.add();
}
}
8. Inheritance
acquiring the properties of a class is called inheritance.
The class that is inherited is called a superclass.
The class that does the inheriting is called a subclass.
For inheriting the property extends keyword is used.
class mca_1st_year {
void add(){
System.out.println("super class");
}
}
public class a extends mca_1st_year{ //inheritance
void add(){
//super.add(); // for calling parent class method
System.out.println("sub class");
}
public static void main(String args[]){
a obj = new a();
obj.add();
}
}
9. Inheritance
Types of inheritance:
Single inheritance
Multilevel inheritance
Hierarchal inheritance
Multiple inheritance (not supported in java)
Try coding example of each…
10. Encapsulation
Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse.
Encapsulation is achieved by using different access modifiers :
public
private
protected
default (no modifier)
13. How to create our own package (user
defined package) :
package is collection of classes.
package keyword is used.(eg. package “mca_1”)
For importing user defined package:
import packageName.className; //syntax
import mca_1.classname;
We will study packages in next unit….
14. Methods
a block of code that runs only when it is called.
type name(parameter-list) //syntax
{
// body of method
}
15. Calling a method :
Box b= new Box();
b.volume();
b.setDim(1.0,1.0,2.0);
∞ Method overloading – same name with different parameters
within same class (ex. compile time Polymorphism)
∞ Method overriding – same name same parameters but in different
classes (ex. Inheritence i.e. run time polymorphism)
16. Constructors
special type of methods whose name is same as the class
name and have no return type.
The constructor is automatically called when the object is created
Types of constructor :
Default constructor
Parameterised constructor
Copy constructor
18. Parameterised constructor
class a{
a(int a , int b){
System.out.println("parameterised constructor " + (a+b));
}
}
public class cons {
public static void main(String arg[]){
a obj1;
obj1 = new a(10,30);
}
}
19. Copy constructor
Import java.io.*;
class a{
String s;
int age;
a(String s , int age){
this.s= s;
this.age=age;
}
a(a ob){
this.s=ob.s;
this.age=ob.age;
System.out.println("copy constructor :"+s+" age :"+age);
}
}
public class cons {
public static void main(String arg[]){
a obj3=new a("sukanya", 26);
a obj4= new a(obj3); //copy
}
}