













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














public class student { String name; //Data Members int age; //Data Members public void setData(String n, int a) //Member Functions { name = n; age = a; } public void getData() //Member Functions { System. out .println("Name s :"+name); System. out .println("age is :"+age); } public static void main (String[] arrgs) // Main function { student obj= new student(); // Object Creation obj.setData("abc", 10); // Method Calling using object obj.getData(); // Method Calling using object } }
public abstract class student { // making class abstract using abstract keyword String name; int age; public void setData(String n, int a) { name= n; age= a; } public void getData() { System. out .println("Name s :"+name); System. out .println("age is :"+age); } public static void main (String[] arrgs) { student obj= new student(); /* Following is not allowed and raise error */ obj.show(); } }
public class student { private String name; //Private Data Members private int age; //private Data Members public student() //constructor { name="abc"; age=0; } public void setData(String n, int a) //Member Functions { name = n; age = a; } public void getData() //Member Functions { System. out .println("Name s :"+name); System. out .println("age is :"+age); } public static void main (String[] arrgs) // Main function { student obj= new student(); // Object Declaration obj.setData("abc", 10); // Method Calling using object obj.getData(); // Method Calling using object }
Multiple Inheritance
Multilevel Inheritance
public class A { public void methodA() { System. out .println("Base class method"); } } public class B extends A { public void methodB() { System. out .println("Child class method"); } public static void main(String args[]) { B obj = new B(); obj.methodA(); //calling super class method obj.methodB(); //calling local method } }
Hierarchical Inheritance
Hybrid inheritance
class A { public void methodA() { System. out .println("Class A method"); } } class B extends A { public void methodB() { System. out .println("class B method"); } } class C extends B { public void methodC() { System. out .println("class C method"); } public static void main(String arrgs[]) { C obj = new C(); obj.methodA(); //calling grand parent class method obj.methodB(); //calling parent class method obj.methodC(); //calling local method } }
public static void main (String[] arrgs) // Main function { A obj= new C(); // upcasting from subclass to super class obj.display(); // Method Calling of class C B objB=(B) obj; //Downcasting of reference to subclass reference objB.display(); //method calling of class C }
Wrapper Classes, Boxing & Unboxing, Packages Wrapper Classes
int x = 25; Integer y = new Integer(33); interface vehicle { /* All of the methods are abstract by default */ public void accelerate(); public void breaking(); } class CAR implements vehicle { public void accelerate() { System. out .println("Car is accelerating"); } public void breaking() { System. out .println("Break applied"); } public static void main(String[] arrgs) { CAR obj= new CAR(); //object of Class CAR obj.accelerate(); obj.breaking(); } }
try { //Do something } catch (Exception ex) { //Catch exception hare using exception argument ex }
public class ExceptionSample { public static void main (String[] arrgs) // Main function { int no1, no2; //Data variables no1=5; //Contain some value no2=0; //contain some value try // Try block { System. out .println(no1/no2); //attempt to divide number by 0 } catch (Exception ex) // Catch block receive exception { /* Display exception message */ System. out .println("Error with defination: "+ex.getMessage()); } } }
1: Reading information into program using StreamReader 2: Writing information from program using StreamWriter
Reading Text File
import java.io.*;
catch (Exception ex) { System. out .println("Error writing to file"); } } public static void main (String[] arrgs) { fileHandling obj= new fileHandling(); obj.WriteFile("c:\MyFile.txt"); } }
public static void main (String[] arrgs) { ArrayList arr= new ArrayList(); arr.add("A"); arr.add("B"); arr.add("C"); arr.add("D"); arr.add("E"); //Using foreach over collection for (Object s : arr) { System. out .println(s); }
public class TimeClass{ private int hour; private int minute; private int second; public TimeClass() { hour = minute = second = 0; }
point obj= new Circle(); // up-casting from subclass to super class obj.display(); // Method Calling of class circle } }
import java.awt.event.; import javax.swing.; public class LoanCalc extends JFrame{ private JButton btnCalc; private JTextField number1; private JTextField number2; private JTextField op; private JTextField result; private JLabel lable1; private JLabel lable2; private JLabel lable3; private JLabel lable4; LoanCalc() { setTitle("Basic Calculator"); setBounds(200,200,400,500); btnCalc= new JButton("Calculate"); number1= new JTextField(); number2= new JTextField(); op= new JTextField(); result= new JTextField(); lable1= new JLabel("First Number"); lable2= new JLabel("Second Number"); lable3= new JLabel("Operation"); lable4= new JLabel("Result"); setLayout( null ); number1.setBounds(120, 100, 200, 50); number2.setBounds(120, 150, 200, 50); op.setBounds(120, 200, 200, 50); result.setBounds(120, 350, 200, 50); lable1.setBounds(20,100, 100,50); lable2.setBounds(20, 150, 100, 50); lable3.setBounds(20, 200, 100, 50); lable4.setBounds(20, 350, 100, 50);
btnCalc.setBounds(150, 280, 100, 50); btnCalc.addActionListener( new listner()); btnCalc.setActionCommand("Calculate"); add(number1); add(number2); add(op); add(result); add(btnCalc); add(lable1); add(lable2); add(lable3); add(lable4); } public class listner implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String btn=e.getActionCommand(); int no1,no2, ans=0; String opration; if (btn=="Calculate") { no1= Integer. parseInt (number1.getText()); no2= Integer. parseInt (number1.getText()); opration= op.getText(); switch (opration) { case "+": ans=no1+no2; break ; case "-": ans=no1-no2; break ; case "": ans=no1no2; break ; case "/": if (no2>0) ans=no1/no2; else ans=0; break ; default : ans=0;