Java Essentials PPT Lesson02
Java Essentials PPT Lesson02
Java Constructors
Platform
independent
Distributed
Robust
design
Features
of Java
Object- Internet
oriented friendly
Multi
threaded
Constructors are methods used to initialize values of the object when instantiated.
int a;
int b;
Const_Demo()
{
a=100;
System.out.println("Variable a
initialized to "+a);
b=200;
System.out.println("Variable b
initialized to "+b);
}
public static void main(String
args[]){
Const_Demo c1 = new Const_Demo();
}
}
int a;
int b;
Const_Demo()
{
a=100;
System.out.println("Variable a initialized to "+a);
b=200;
System.out.println("Variable b initialized to "+b);
}
Const_Demo(int x,int y){
a=x;
b=y;
System.out.println("Variable a initialized to "+a);
System.out.println("Variable b initialized to "+b);
}
public static void main(String args[]){
Const_Demo c1 = new Const_Demo();
Const_Demo c2 = new Const_Demo(10,20);
}
Packages are:
• Logical grouping of classes and interfaces.
• Created to avoid naming conflicts among different classes.
• Predefined in Java.
• Analogous to directories of files, where the directories comprise class files.
All the packages are located relative to the base_location of Java in the file system.
The process of redefining the method in the child class is known as method overriding.
To override a method, the method definition in the child class must have the same signature as that in the base class.
The super keyword is used to access the method of the base class, which is also referred to as super class at times.
Once the child class Cylinder is created, the main method program is written as shown in the code here.
import Geometry.*;
public class MetOverDemo {
public static void main(String
args[]){
Cylinder c1 = new
Cylinder(3,5);
double res = c1.area();
Circle c2 = new Circle(3);
System.out.println(res);
double res2 = c2.area();
System.out.println(res2);
}
}
Abstract class is one which has abstract methods. In abstract methods, the method declaration is present but not the
definitions.
Such methods are represented by prefixing ‘abstract’ keyword to the method.
Syntax:
abstract class class_name{…}
abstract return_type method_name(parameter_list);
An interface is an abstract class, where all the methods are abstract methods. Following are the features of interfaces:
If an interface has a method definition, then the method is a static final method.
Any class which uses the interface implements the interface, here ‘implements’ is a keyword like
‘extends’.
The child class which is implementing an interface has to define all the abstract methods of the
interface otherwise the child class becomes an abstract class and cannot be instantiated.
The interface created earlier can be implemented by a class and the methods of the interface can be invoked
through the instances of the class.
}
}
In Java:
– Input and output are implemented as streams.
– Streams are handled through classes such as BufferedStream, InputStream, and OutputStream.
– Formatted data is handled through Scanner classes.
– The input and output functions are implemented through the packages java.io.
int c;
System.out.print("Enter a
character:");
c = System.in.read();
System.out.println("You entered " +
c);
}
}
Input and output in Java is handled through one of the following streams of data:
An additional buffer is
These streams are
Read data one allocated for the
used to read data in
character at a time. stream in the main
bytes.
memory.
while(st.hasMoreTokens())
pro =
pro*(Integer.parseInt(st.nextToken()));
System.out.println("The product
is: " + pro);
}
}
The most common way of writing output to the console is through System.out.print() and System.out.println() methods.