JAVA By, RAJKIRAN EDUNURI B.Tech Computer Science, Guru Nanak Institutions,Hyderabad e- mail : rajkiranedunuri97@gmail.com Website : rockzzweb.wordpress.com
JAVA What is JAVA Why Java has Become More Popular *** Java is Case Sensitive language
What is JAVA Simply, JAVA is an “OBJECT ORIENTED PROGRAMMING LANGUAGE”
What is OBJECT ORIENTED PROGRAMMING (OOPS) (Important for interviews) • Object Oriented Programming is a programming method that combines : • A) Data • B) Instructions For processing that data into a self-sufficient ‘Object’ that can be used within a program or in other programs..
CONCEPTS IN OBJECT ORIENTED PROGRAMMING .. • Classes • Objects • Abstraction • Inheritance • Polymorphism • Encapsulation • This Concepts Make The Java An “ OOP Language..”
Why Should i learn OOPS • OOPS Make Development and maintenance Easy.. • OOPS Provides Data Hiding • OOPS Provide Ability to Simulate Real-World event much more Efficiently.
Java Editor or IDE’S • ECLIPSE • NETBEANS • TEXT EDIT, J-EDIT. -> FOR MAC Operating System.. • Notepad/ wordpad, Notepad++ For Windows platform
Structure of Java program class class_name { public static void main (String [ ] args ) { // Statement 1; // Statement 2; } } Java is Case Sensitive language
Simple Java Hello World Program public class hello_world { public static void main (String[ ] args) { System.out.println(“ HELLO WORLD ”); } } Save Above program with “ hello_world.java “ without Quotes Output : HELLO WORLD
Explanation of Previous program • As you Know that The O/P of Above program is “ HELLO WORLD” • Public -> Access Modifier, If we declare any method/variable or any class as public, it means that we can access that method/class anywhere in the program… • Class -> collection of methods, variables, constructors, etc.. ( in java we write any method/any logic inside the class ) • hello_world -> name of the class… • Public static void main (String [ ] args ) -> Main method • System.out.println( ) -> used to print Any text,Values etc on Screen..
What is a METHOD…??? • In general, method is nothing but a Function.. • Block of statements written in a Separate prototype.. • Syntax : • Data_type method_name() { Statement1; Statement2; } • Example : void abstraction () { statement1; statement2; }
CLASS • Simply, Collection of methods, variables, constructors, etc.. is called CLASS • In java, we write any method/constructor, variables etc inside of the class.. • Syntax : • class class_name { • Ex : class hello_world { • NOTE : In java, Main class (sub class) name & file name should be same, else it will throw an exception that “ Class names Not matched”…
OBJECTS • Simply, Object is an Bundle of Related Variables and Methods • Objects are used to Access methods , variables, constructors Present inside a class , in other class… • Syntax : class_name object_name = new class_name(); •
Program to understand concept of Objects class my_bio { /* Writing method */ /* method is nothing but a function to perform some operation */ void bio_data () { /* Creating method */ String name=“shashi”; String college=“Malla Reddy Institute of technology”; int age=20; System.out.println(“Name : ”+name); System.out.println(“ College : ”+college); System.out.println(“Age : ” +age); } public static void main (String [] args ) { my_bio ob =new my_bio(); /* Created Object to access method */ ob.bio_data(); /* calling method using object */ } } Save Above with “ my_bio.java ”
Output : Name : shashi college : Malla Reddy Institute of technology Age : 20 • In the Previous Program, we have created a class and inside the class we have written Some code to the method ( public void bio_data () ) • Then, we have written main method, created object ( ob ) to class (my_bio) and Called method using “ object_name.method_name(); ” which Results the Above Output..
ABSTRACTION • Abstraction in the sense, “ HIDING THE DATA ” • It Allows us to Hide Some Data and Reveal only Required data.. • In Some cases, user want only some data from a large data.. • In that cases, Abstraction Becomes handy…
Program tp Understand concept of ABSTRACTION class Abstraction { Public static void main(String [ ] args ) { String name=“Shashi”; String college=“Malla Reddy Institute of Technology”; int age=20; String address=“HYDERABAD”; System.out.println(“Name : ”+name); System.out.println(“College : ”+college); System.out.println(“Address : ”+ address); } } Save Above Program with “ Abstraction.java ”
OUTPUT : Name: Shashi College : Malla Reddy Institute of Technology Address : HYDERABAD • In the Previous Program, we have declared 4 instances variables ( name, college, age,Address) • But we have printed only Name,College,Address ,but not Age • As i require only name,address,college, i displayed them and hid the Age variable.. • This Process is called ABSTRACTION…
POLYMORPHISM • Performing Different Operation On Different methods but with same Method Name.. • Simply, we can say that the “METHOD NAMES WILL BE SAME BUT OPERATIONS PERFORMED ON METHOD WILL BE DIFFERENT ”
Program to Understand Polymorphism class polymorphism { void calculate(int x) { return sqrt(x); } void calculate(int x) { return (x*x); } Public static void main(String [ ] args ) { Polymorphism ob =new polymorphism(); System.out.println(“Square Root is : ”+ob.calculate(25)); System.out.println(“Square of x is : ”+ob.calculate(2)); } } Save Above with “ polymorphism.java ”
Output : Square Root is 5 Square is 4 • In the previous program, we have created class (polymorphism) and created 2 methods (calculate) with Same name but operations performed on them was Different.. • In first method calculate, we performed Square Root of a number.. • In Second Method calculate, we performed Squaring of a number..
ENCAPSULATION • Simply, Process of Binding The Data of variables or methods is called ENCAPSULATION • In Encapsulation, Names of variables will be same, but values of them will be different..
Program to Understand ENCAPSULATION class a { /* Super Class 1 */ String name=“Shashi”; String college=“Malla Reddy Institute of Technology”; void method1(){ System.out.println(“Name : ”+name); System.out.println(“College : ”+college); } } class b { /* Super class 2 */ String name=“Rajkiran”; String college=“Guru Nanak Institutions”; void method2() { System.out.println(“Name : ”+name); System.out.println(“College : ”+college); } } class encapsulation { /* Sub class */ public static void main(String [ ] args ) { a ob1=new a(); b ob2=new b(); ob1.method1(); ob2.method2(); } } Save with “ encapuslation.java ” as main method is present in sub class
Output : Name : shashi College : Malla Reddy Institute of Technology Name : Rajkiran College : Guru Nanak Institutions • In The Previous Program, we have Declared 3 classes ( 2 super and 1 sub class) Created 1 method in each Super class (variable names are same, but values of them are Different..) • we have called the methods using objects in sub class, which results above output…
INHERITANCE ( Important ) • The Process by which one class acquires the properties of another class… • Simply,In Inheritance, we can use the properties one class in some other class or more than one class • Keyword : “ extends ”
Program to understand INHERITANCE class a { void method1() { /* creating a method */ String name=“Shashi”; String college=“Malla Reddy Institute of Technology”; System.out.println(“Name : ”+name); System.out.println(“College : ”+college); } } class b extends a { void method2() { /* creating a method */ String name=“Rajkiran”; String college=“Guru Nanak Institutions”; System.out.println(“Name : ”+name); System.out.println(“College : ”+college); } } class inheritance { public static void main(String [ ] args ) { a ob1=new a(); /* Object to class a */ b ob2=new ob2(); /* Object to class b */ ob1.method1(); ob2.method2(); ob2.method1(); /* calling method1 which is present in class a with ob2 */ } } Save code with “ inheritance.java ”
Output Name : Shashi College : Malla Reddy Institute of Technology Name : Rajkiran College : Guru Nanak Institutions Name : Shashi College: Malla Reddy Institute of Technology
Recommend
More recommend