0% found this document useful (0 votes)
4 views

Unit 1 JAVA

The document provides a series of Java programs demonstrating core Object-Oriented Programming (OOP) concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It includes examples of class properties, methods, method overloading, and method overriding, along with their respective outputs. Each section illustrates the practical application of these concepts through simple code snippets.

Uploaded by

manesneharajesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 1 JAVA

The document provides a series of Java programs demonstrating core Object-Oriented Programming (OOP) concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It includes examples of class properties, methods, method overloading, and method overriding, along with their respective outputs. Each section illustrates the practical application of these concepts through simple code snippets.

Uploaded by

manesneharajesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit 1 : JAVA Program

1. // Use of Object and Classes in Java


import java.io.*;
class Numbers {
// Properties
private int a;
private int b;

// Methods
public void sum() { System.out.println(a + b); }

public void sub() { System.out.println(a - b); }

public static void main(String[] args)


{

// Creating Instance of Class


// Object
Numbers obj = new Numbers();

// Assigning Values to the Properties


obj.a = 1;
obj.b = 2;

// Using the Methods


obj.sum();
obj.sub();
}
}

Output
3
-1

2. Java Program to demonstrate Use of Class and Objects


// Class Declared
public class GFG {
// Properties Declared

static String Employee_name;

static float Employee_salary;


// Methods Declared

static void set(String n, float p) {

Employee_name = n;

Employee_salary = p;
}

static void get() {

System.out.println("Employee name is: " +Employee_name );

System.out.println("Employee CTC is: " + Employee_salary);

}
// Main Method

public static void main(String args[]) {

GFG.set("Rathod Avinash", 10000.0f);

GFG.get();

Output

Employee name is: Rathod Avinash

Employee CTC is: 10000.0

Method and Method Passing


// Class Method and Method Passing

import java.io.*;

class Student {
// Properties Declared

int id;

String name;
// Printing Student

public void printStudent()


{

System.out.println("Id:" + id);

System.out.println("Name:" + name);

class GFG {

public static void main(String[] args)

Student obj = new Student();

obj.id = 1;

obj.name = "ABC";

obj.printStudent();

}
Output

Id:1

Name: ABC

Java OOPs Concepts

1. Abstraction
// abstract class
abstract class GFG {
// abstract methods declaration

abstract void add();

abstract void mul();

abstract void div();

2. Encapsulation
// Encapsulation using private modifier

// Employee class contains private data

// called employee id and employee name

class Employee {

private int empid;

private String ename;


// Setter methods

public void set_id(int empid) {

this.empid = empid;

public void set_name(String ename)

this.ename = ename;

// Getter methods

public int get_id() {

return empid;

}
public String get_name() {
return ename;

public class Geeks {

public static void main(String args[])

Employee e = new Employee();


e.set_id(78);

e.set_name("John");

System.out.println("Employee id: " + e.get_id());

System.out.println("Employee Name: " + e.get_name());

Output

Employee id: 78

Employee Name: John

3. Inheritance

//base class or parent class or super class

class A {
//parent class methods

void method1(){}
void method2(){}
}
//derived class or child class or base class

class B extends A { //Inherits parent class methods

//child class methods

void method3(){}

void method4(){}

4. Polymorphism

sleep(1000) //millis

sleep(1000,2000) //millis,nanos

Types of Polymorphism

Method Overloading and Method Overriding

// Java Program to Demonstrate

// Method Overloading and Overriding

// Parent Class

class Parent {
// Method Declared

public void func(){

System.out.println("Parent Method func");

}
// Method Overloading

public void func(int a){

System.out.println("Parent Method func " + a);

}
// Child Class

class Child extends Parent {


// Method Overriding

@Override

public void func(int a){

System.out.println("Child Method " + a);

}
// Main Method

public class Main {


public static void main(String args[]){

Parent obj1 = new Parent();

obj1.func();

obj1.func(5);

Child obj2 = new Child();

obj2.func(4);

Output

Parent Method func

Parent Method func 5

Child Method 4

You might also like