class prg {
int n,n2,sum;
public void take(int x,int y) {
n=x; n2=y;
}
public void sum() {
sum=n+n2;
}
public void print() {
System.out.println("The Sum is"+sum);
}
}
class prg1 {
public static void main(String args[]) {
prg obj=new
prg();
obj.take(10,15);
obj.sum();
obj.print();
}
}
class Demo1 {
int n, n2;
public void take(int x, int y) {
n = x;
n = y;
}
public int process() {
return (n + n2);
}
}
class prg {
public static void main(String args[]) {
int sum;
Demo1 obj = new Demo1();
obj.take(15, 25);
sum = obj.process();
System.out.println("The sum is" + sum);
}
}
class prg1 {
int x = 5, y = 5, z = 0;
public void sum() {
z = x + y;
System.out.println("Sum is " + z);
}
public void sum(int a, int b) {
x = a;
y = b;
z = x + y;
System.out.println("Sum is " + z);
}
public int sum(int a) {
x = a;
z = x + y;
return z;
}
}
class Demo {
public static void main(String args[]) {
prg1 obj = new prg1();
obj.sum();
obj.sum(10, 12);
System.out.println(+obj.sum(15));
}
}
class para123 {
int n, n2, sum, mul;
public void take(int x, int y) {
n = x;
n2 = y;
}
public void sum() {
sum = n + n2;
System.out.println("The Sum is" + sum);
}
public void take2(para123 obj) {
n = obj.n;
n2 = obj.n2;
}
public void multi() {
mul = n * n2;
System.out.println("Product is" + mul);
}
}
class DemoPara {
public static void main(String args[]) {
para123 ob = new para123();
ob.take(3, 7);
ob.sum();
ob.take2(ob);
ob.multi();
}
}
A static data field does not belong to any one object
Also called a class variable
Only one instance of the variable exists for all instances of the class
Note that a static data field is not a constant (final)
All instances of the class reference that one variable
Consider the need of a method that does not belong to an object of any type
When specified static, a method is still a member of the class
Call with the name of the class
int maximum = Math.max(2, 3);
double root = Math.sqrt(4.2);
this
keyword in Javathis
is a reference variable that refers to the current object.//Java code for using 'this' keyword to
//refer current class instance variables
class Test {
int a;
int b;
// Parameterized constructor
Test(int a, int b) {
this.a = a;
this.b = b;
}
void display() { //Displaying value of variables a
and b
System.out.println("a = " + a + " b = " +
b);
}
public static void main(String[] args) {
Test object = new Test(10, 20);
object.display();
}
}
Array is collection of related data items
Declaring an Array:
<type> [ ] variable_name;
Double[ ] myList;
double myList[ ];
Both syntaxes are equivalent
No memory allocation at this point
Defining an Array:
variable_name=new <type>[arraySize];
Number = new int[5];
Mylist = new int[10];
dataType[arraySize];
variable_name
dataType array_name[ ] = {list of values};
Int a [ ]={1,2,3,4,5,6,7,};
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ArraySizeInput {
public static void main(String[] args) throws Exception {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String inData;
int num;
System.out.print("Enter a Size for Array:");
inData = stdin.readLine();
num = Integer.parseInt(inData);
long[] primes = new long[num];
System.out.println("Array Length = " + primes.length);
}
}
// OUTPUT
// Enter a size for array: 4
// Array Length = 4
public class TestArray {
public static void main(String[] args) {
double[] myList = {
1.9,
2.9,
3.4,
3.5
};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}
// OUTPUT
// 1.9
// 2.9
// 3.4
// 3.5
`
long[] primes = new long[20];
primes[0] = 2;
primes[1] = 3;
System.out.println(primes[0]);
System.out.println(primes[1]);
// Output:
// 2
// 3
length()
methodlong[ ] primes = new long[20];
System.out.println(primes.length);
// OUTPUT
// 20
public class MinArray {
public static void main(String[] args) {
int[] array = { 20, 19, 1, 5, 71, 27, 19, 95 };
int min = array[0];
for (int index = 1; index < array.length; index++) {
if (array[index] < min) {
min = array[index];
}
}
System.out.println("The minimum of this array is: " + min);
}
}
Int myarray[][];
Myarray = new int[3][4];
Int myarray [][] = new int[3][4];
Int myarray[2][3]={0,0,0,1,1,1};
2 columns and 3 rowsfor(data_type variable : array | collection) {
//body of for-each loop
}
void method_name (int [] array);
method_name
will accept an array parameter of type int
. So if you have an int array named myarray
, then you can call the above method as follows: method_name (myarray);
class CommandLineExample {
public static void main(String args[]) {
System.out.println("Your first argument is: " + args[0]);
}
}
javac CommandLineExample.java
java CommandLineExample sonoo
Your first argument is: sonoo
extends
.class derived-class extends base-class {
//methods and fields
}
class A {
int x, y;
void getdata(int a, int b) {
x = a;
y = b;
}
int add() {
System.out.println(“Superclass Method”);
return (x + y);
}
}
class B extends A {
int mult() {
System.out.println(“Sub - class Method”);
return (x * y);
}
}
class Test {
public static void main(String args[]) {
B obj = new B();
int add, mult;
obj.getdata(5, 3);
add = obj.add();
mult = obj.mult();
System.out.println(“Addition” + add);
System.out.println(“Multiplication” + mult);
}
}
class one {
public void print() {
System.out.println(“hi ");
}
}
class two extends one {
public void print1() {
System.out.println(“hello ");
}
}
class three extends two {
public void print2() {
System.out.println(“hi ");
}
}
// Drived class
public class Main {
public static void main(String[] args) {
three g = new three();
g.print();
g.print1();
g.print2();
}
}
class One {
int x = 10, y = 20;
void disp() {
System.out.println(“Method of class One”);
System.out.println(“Value of X” + x);
System.out.println(“Value of Y” + y);
}
}
class Two extends One {
void add() {
System.out.println(“Method of class Two”);
System.out.println(“Addition: ”+(x + y));
}
}
class Three extends One {
void mul() {
System.out.println(“Method of class Three”);
System.out.println(“Multiplication: ”+(x * y));
}
}
class Test {
public static void main(String args[]) {
Two obj1 = new Two();
Three obj2 = new Three();
obj1.disp();
obj1.add();
obj2.mul();
}
}
class Student {
int m1, m2;
void getmarks(int x, int y) {
m1 = x;
m2 = y;
}
void putmarks() {
System.out.println(“First” + m1);
System.out.println(“Second” + m2);
}
}
interface sport {
int sp = 6;
void spmarks();
}
class Result extends Student implements
Sport {
int total;
public void spmarks() {
System.out.println(“Sport Mark” + sp);
}
void disp() {
putmarks();
spmarks();
total = m1 + m2 + sp;
System.out.println(“Total” + total);
}
}
class Test {
public static void main(String args[]) {
Result obj = new Result();
obj.getmarks(80, 60);
obj.disp();
}
}
class C {
public void display() {
System.out.println("C");
}
}
class A extends C {
public void display() {
System.out.println("A");
}
}
class B extends C {
public void display() {
System.out.println("B");
}
}
class D extends A {
public void display() {
System.out.println("D");
}
}
class Main {
public static void main(String args[]) {
D obj = new D();
obj.display();
}
}
Why we need an Abstract Class?
abstract class Animal { //abstract method
public abstract void sound();
}
public class Dog extends Animal //Dog class
extends Animal class {
public void sound() {
System.out.println("Woof");
}
public static void main(String args[]) {
Dog obj = new Dog();
obj.sound();
}
}
Why can’t we create the object of an abstract class?
Keypoints:
Animal.staticMethod();
Difference between Abstract Class and Interface
Abstract Class | Interface |
---|---|
Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. |
Abstract class doesn't support multiple inheritance. | Interface supports multiple inheritance. |
Abstract class can have final, non-final, static and non-static variables. | Interface has only static and final variables. |
Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
An abstract class can be extended using keyword "extends". | An interface can be implemented using keyword "implements". |
A Java abstract class can have class members like private, protected, etc. | Members of a Java interface are public by default. |
public abstract class Shape{ public abstract void draw(); } | public interface Drawable{ void draw(); } |
When to use Abstract Class and Abstract Method?
// By using different types of arguments
class MultiplyFun {
// Method with 2 parameter
int Multiply(int a, int b) {
return a * b;
}
// Method with the same name but 2 double parameter
double Multiply(double a, double b) {
return a * b;
}
}
class Main {
public static void main(String[] args) {
MultiplyFun f = new MultiplyFun();
System.out.println(f.Multiply(2, 4));
System.out.println(f.Multiply(5.5, 6.3));
}
}
// By using different numbers of arguments
class MultiplyFun {
// Method with 2 parameter
int Multiply(int a, int b) {
return a * b;
}
// Method with the same name but 3 parameter
int Multiply(int a, int b, int c) {
return a * b * c;
}
}
class Main {
public static void main(String[] args) {
MultiplyFun f = new MultiplyFun();
System.out.println(f.Multiply(2, 4));
System.out.println(f.Multiply(2, 7, 3));
}
}
class OperatorOVERDDN {
void operator(String str1, String str2) {
String s = str1 + str2;
System.out.println("Concatinated String - " +
s);
}
void operator(int a, int b) {
int c = a + b;
System.out.println("Sum = " + c);
}
}
class Main {
public static void main(String[] args) {
OperatorOVERDDN obj = new
OperatorOVERDDN();
obj.operator(2, 3);
obj.operator(“a ", “b");
}
}
class Parent {
void Print() {
System.out.println("parent class");
}
}
class subclass1 extends Parent {
void Print() {
System.out.println("subclass1");
}
}
class subclass2 extends Parent {
void Print() {
System.out.println("subclass2");
}
}
class TestPolymorphism3 {
public static void main(String[] args) {
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}
/* Base class Person */
class Person {
void message() {
System.out.println("This is person class");
}
}
class Student extends Person
/* Subclass Student */
{
void message() {
System.out.println("This is student class");
}
void display() // Note that display() is
only in Student class {
message(); // will invoke or call current
class message() method
super.message(); // will invoke or call
parent class message() method
}
}
class Test /* Driver program to test */ {
public static void main(String args[]) {
Student s = new Student();
s.display(); // calling display() of
Student
}
}
class Person {
// Superclass Person
Person() {
System.out.println("Person class Constructor");
}
}
class Student extends Person {
// Subclass Student extending Person
Student() {
super();
System.out.println("Student class Constructor");
}
}
class Test {
// Driver class to test
public static void main(String[] args) {
Student s = new Student();
}
}
this
keyword ? Explain it with example.for each
loop with example.Super
keyword? Explain it with its method.Super
keyword with constructor.Made By SOU Student for SOU Students