The document discusses key concepts in Java including classes, objects, variables, data types, comments, access modifiers, inheritance, and more. It provides examples of how to define classes with methods and variables, create objects, access instance variables and methods, use access modifiers like public and private, extend classes, and override methods. Constructors are shown being used to initialize objects.
2. Java supports single-line and multi-line comments that is
very similar to c and c++
public class MyFirstJavaProgram{
/* this is my first java program
* this will print Hello World as output
* this is an example of multi-line comments
*/
public static void main(String args[])
{
//This is an Example of a single line comment
/* This is also an example of a single line comment */
}
3. Class is a blue print from which individual objects are created.
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
}
4. Object is nothing but an instance of a class
Objects have both state and behaviors
Ex:
If we consider dog then its state is – name ,
breed and the behavior is barking, wagging ,running
5. public class Puppy {
Public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :"+ name );
}
public static void main(String[]args) {
// Following statement would create an object myPuppy
Puppy myPuppy =new Puppy("tommy");
}
}
6. A class contain any of the following variable types
Local Variables
Instance Variables
Class Variables
7. public class Test{
public void pupAge(){
int age =0;
age = age +7;
System.out.println("Puppy age is : "+age);
}
public static void main(String args[]){
Test test =new Test();
test.pupAge();
}
}
8. public class Test{
int age;
public void pupAge(){
System.out.println("Puppy age is :"+age);
}
public static void main(String args[]){
Test test =new Test();
test.pupAge();
}
}
9. import java.io.*;
public class Employee{
public static final String DEPARTMENT="Development ";
public static void main(String args[]){
salary =1000;
System.out.println(DEPARTMENT+"averagesalary:"+sa
lary); }
}
10. public class Puppy {
public puppy(){
}
public puppy(String name) {
// This constructor has one parameter, name.
}
}
11. Visible to the package, the default. No modifiers are
needed.
Visible to the class only (private).
Visible to the world (public).
Visible to the package and all subclasses (protected).
12. package cert;
public class Sludge {
public void testIt() {
System.out.println("sludge");
}
}
package book;
import cert.*; // Import all classes in the cert package
class Goo {
public static void main(String[] args) {
Sludge o = new Sludge();
o.testIt();
}
}
13. public classLogger{
private String format;
public String getFormat()
{
return this.format;
}
public void setFormat(String format){
this.format = format;
}
}
14. package cert;
class Beverage {
}
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage {
}
Can't access class cert.Beverage. Class or interface must
be public, in same package, or an accessible member
class.
17. package cert;
public final class Beverage {
public void importantMethod() {
}
}
package exam.stuff;
import cert.Beverage;
class Tea extends Beverage {
}
Can't subclass final classes: class
cert.Beverage class Tea extends Beverage{
1 error
18. Class Bank{
int accountBalance ;
public Bank(){
accountbalance = 10000;
}
Synchronized void BalanceInquiry(){
System.out.println(“account balance is
:”+accountbalance);
}
}
19. abstract class Hotel
{
public void Idli(){
System.out.println(“Prepare Idli”);
}
abstract public void Dosa();
}
20. A literal is source code representation of a fixed
value
Literals can be assigned to any primitive type
variable
Ex:
byte a = 68;
int decimal = 100;
String name = “shridhar”
21. Reference Data Types:
Reference variables are created using defined
constructors of the classes. They are used to access
objects. These variables are declared to be of a specific
type that cannot be changed. For example, Employee,
Puppy, etc.
Class objects and various types of array variables come
under reference data type.
Default value of any reference variable is null.
A reference variable can be used to refer to any object of
the declared type or any compatible type.
Example: Animal animal = new Animal("giraffe");
22. Instance variables and methods are accessed via
created objects.
/* First create an object */
ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/* Now you can call a class method as follows */
ObjectReference.MethodName();
23. public class Puppy{
int puppyAge;
public Puppy(String name){
System.out.println("Passed Name is :"+name );
}
public void setAge(int age ){
puppyAge = age;
}
publicint getAge(){
System.out.println("Puppy's age is :"+ puppyAge );
return puppyAge;
}
24. public static void main(String[]args){
Puppy myPuppy =newPuppy("tommy");
myPuppy.setAge(2);
myPuppy.getAge();
/* You can access instance variable as follows as
well */
System.out.println("Variable Value :"+
myPuppy.puppyAge );
}
}