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

Java q Spider

The document provides an overview of Java programming, including its history, features, and structure. It explains different types of programming languages, Java's object-oriented nature, and key concepts such as data types, variables, operators, and methods. Additionally, it covers decision-making statements, looping statements, and method terminologies in Java.

Uploaded by

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

Java q Spider

The document provides an overview of Java programming, including its history, features, and structure. It explains different types of programming languages, Java's object-oriented nature, and key concepts such as data types, variables, operators, and methods. Additionally, it covers decision-making statements, looping statements, and method terminologies in Java.

Uploaded by

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

JAVA Q Spider

Binary: - (0,1) 0 off 1 on


Language: - it is a medium to communicate
Programing language: - it is medium to communicate with machine
In programing language is 3 types
1. Hight
2. Mid
3. Low-level
High level: - high level language understand by humans not by machine
Ex: - java, python, c#, etc.
Mid-level: - it’s also known as assembly level language it is not understanding by humans and machine
Ex: - micro process
Low-level: -It’s also known as binary language machine level language it’s combination of (0,1) this language only
understands by machine
history of java
 Jams gosling is founder of java
 The fast name of java is green talk,
 The second name of java is hock,
 The third name of java is java
 Sun micro system company sated by java
 Right now, java is used by owned oracle
Feature of java
 Java has easy syntax
 Java is platform independent language
 java application developed by java
 Java can be built in any operating system as well as it can be run in any operating system
What is object
 Java is object oriented
 Java deals with real time object and it in takes state & behavior on object
 Java is robots in nature – java is storing in negative it can handle huge application
 Java as automatic Grabar collector – it removes un wanted memory allocated and also it removes unnamed
memory
 Java is extinction in nature -we can integrate java with others programing language
Structure of java
class Demo
{
Public static void main (String [ ] args )
{
System.out.print ln (“hi”)
}
}
Tokens are the smallest unique off a java program
Types of tokens: -
1. Keywords
2. Identifies
3. Literals
4. Separators
5. Comments
6. Operators
Keywords: -
 Key words are compiler aware words which has predefined meaning there or 50 keywords in java
 Key words are always retuning lower case
Ex: - public, static, void, class
Identifiers: - name given for a java program is called as identifiers
Rules of identifiers: -
 Keywords are not allowed as identifiers (or) class name
 Space or not allowed
 Numbers not allowed at the beginning
 Special charters are not allowed
 $ or _ allowed

Conventions: -
 Convention or company stands flowed to write the java program
 To write a class name we followed by convention name “upper camel case” (every word 1st letter should be
capital)

Comments: -
 Comments are the not executive lines we write and this lines or hidden form the compiler
 In comment we have 2types
 Single line comment
 // Used to hide one line
 Multi line comment
 /* */ used to hide multi lines

Literals : data provide for a java programing called as literals


1. Primitive
2. non-primitive
data has been classifying in to 2types one is primitive data 1 is non-primitive data
primitive data: - single value data is knowing as primitive data and it’s has been classifying in to 3 types that is
numbers: -number is used to store single values decimal non decimal values
charters: - chatters id used to store char type of data that means single length data in clues with in single quotes (‘c‘)
Boolean: - Boolean stores either true (or) falls
No n primitive data: - Non primitive data multiple value data is known as non-primitive data
String: - String is group of charters Includes within double quotation (‘’mahi ‘’)
Separators: - Separators or used to separate or dallied from one java statement to another statement
Ex: - { } . [ ], ; : ( )

Variables
 variable TO container to store in single values
Data types: data type is used to specify what type of data to be store inside a variable
Variable declaration
Syntax: data type variable name ;
EX: Int id;
Variable initialization: syntax
Variable name = value;
Expel: id = 1;
Variable declaration + variable initialization
Syntax: data type variable name = value;
Expel: int id = 1

Data types byte Bits Default values


bye 1byte 8 bits 0
Short 2bytes 16 bits 0
int 4 bytes 32 bits 0
Long 8bytes 64 bits 0
float 4bytes 32 bits 0.0
Double 8bytes 64 bits 0.0
char 2bytes 16 bits Uniqu
Boolean - 1 bit True/falls
String null
Type costing
Converting one data type to another data is known as type costing
In type costing in we have 2types
 Primitive type costing
 Non-primitive type costing
In primitive type costing we have 2 types
 Widening
 Naringin
In no-primitive type costing we have 2 types
 Upcasting
 Down costing

Primitive type costing: - - converting one type of single value to another type is known as primitive cost
type
Note: the order of data is
bye>[shot<char]<int<[long<float]<double
Widening: - converting smaller type of data to larger type know as widening in this widening process the owned to be
or there not be loss any data
It is implicit process: - compiler done work knows implicit process
Explicit: - programmer done work is known as explicit
Class A
{
Public static void main ( ) {
Int a = 10;
Double b = a;
System.out.println (a);//10
System.out.println (b);//10.0
}
}
Narrow type costing: - converting larger data type to smaller data type is known as “narrowing”
With the help of narrowing, we should forcefully store large type of value in small container and this is process which
is done by the programmers is known as explicit process
When we archive narrowing thought a type costing operator and that is ( ) (parenthesis)
EX: - class A {
Public static void main (String [] rags)
{
Int a = 65; widening
char b = (char)a;
Int c = b; narrowing
System.out.println(a);
System.out.println, (b);
System.out.println(c);

What is operators
Operators or free defined symbols do some particular task
Types of operators
 Athematic operators
 Assignment operators
 Unary operators
 Relationally compiler operators
 Logical operators
 Ternary operators
Athematic operators: - it’s a binary operators consist of two operands one operators
It’s used to do some takes using this symbol (+ - * / %)
Assignment operators: - It’s al so binary operators used to assign values
Unary operators: - it’s operators wherein we have single operators
Unary operators we have 2types
 Increment: 1.pre increment 2. post increment
 Pre increment (++A)
 Post increment (A++)
 Decrement 1.pre decrement 2. post decrement
 Pre decrement (--A)
 Post decrement(A--)
A B AND
T T T
T F F
F T F
F F F

Relational or compiler operators: - it is binary operators where in it returns Boolean time data operators which we use or
< > <= >= ==
Not A
Logical operators: - logical operators also binary operators Boolean return symbols || && ! T F
F T
|| &&
!
A B OR
T T T
T F T
F T T
F F F

Ternary operators: - Ternay Has 3operands based on the given conduction it will give you Boolean result true or falls
based on that result it will bind to the option like then it is true option one will be the result when condition is falls
Option data type should be same as well as result container should also have same type of data
Syntax: - (condition)? ‘’option1 “: “option2”;
Class Vote {
Public static void main ()
{
Int age = 25;
String result = (age <=21)? “eligible”: “not eligible”;
System.out.println (result);
Buzz no: - any number that is divisible by 7 or the last digit of a number is 7
Class Buzzi {
Public static void main (String)
{
Int num = 28;
String result = (num%7 == 0 || number / 10== 7) ? “ Buzzi no ”: not “buzzi”;
System.out.println(result);
}
}
Decision making statement: - This are used to print same set of statement based on the condition and decision-making
statement are used in deferent scenarios based on the requirement and the condition
types of decision-making statement
 Simple if
 If else
 If lese if/else if ladder
 Nested if
 Switch case
Simple if: - It operate based on the condition and if the condition is true it is go to print some set of instructions and if
it is false not going to print anything because it only exhibits positive scenarios not the negative
Syntax: - if (condition) {
s.o.println( )
}
If else: - In if else we have both positive and negative scenarios

Class Num{
Main( )
Int num = 11;
If ( num % 2 ==0){
System.out.println(“the num is even”);
}
else {
system.(“ the given num is odd);
}
}
}
If else is: - if else if you want check 2 or more condition then we will use if lese if
Class A {
Main () {
If (condition1) {
Statement1
If else (condition2) {
Statement 2
}
}
 O 90
 A 70 -90
 B 50 -69
 C 35 – 49
 F 0 – 34
Syntax: -
Class {
Main ( ) {
Int marks =85;

Nested if: - we used nested if for 2 step verification


If ( condition ) {
If (condition) {
s.o.println( );
}
Else (condition){
s.o.println( ) ;
}
}
Else {
s.o.println( );
}
}
}5

Looping statement: -
If you want to do a particular task repeated time we go for looping statement
There are 5 types
 While loop
 Do while
 For loop
 Nested loop
 For each

For loop: -
Syntax:
For ( initialization :condition ; updation){
s.o.pln.();
}
Ex:
For ( a =10; a<=100;a++){
s.o.pln(a);
}
While loop : while ( condition ) {
S.o.pln( a);
a++
}
Ex:

Methods:
 Method is block of code to do some specific tasks it is also called as set instruction to do some specific task
 Method also called as functions
Advantages of methods:
1. Code reusability
2. Code modularity
1. Code reusability: we will write the code once and use it many numbers of times due to this there will be the
reduces in memory consumption
2. Code modularity: using methods we keep the code in to smaller modules what it we will be easy for the updates
as well as for testing we no need to test the while application if we update the only modules which are updated
needs to be tested
EX: for a method:
Public static void main (String [] args)
Syntax of method:  [ Access modifier] [modifier] [return type] [method name] [formal arguments]
{
System.out.println()
}
1. Access modifier:
 Access modifier are used to specify the accessibility of a java member we have 4 types
 Public
 Protected
 Private
 Package level (default)
2. Modifiers:
 Modifiers is used to specify the modularity in modifies we have 2 types
1. Static, 2. non-static
Modifiers also called as non-access modifies
1. static: static means one copy for class
2. non- static: non- static means multiple copes based on the object created
3. Return types: return types show cases types of data return by a method it’s has 2types
1. with return types 2. Without return types
1. with return type has all the types of data to be return
2. without return type we used void
4. Method name: method name is identifier the name which we give for a method is method name and we follow
lower canal case syntax to write a method name
5. Formal arguments  formal argument we can write variable declaration array declaration, object references in
formal arguments and it has 2types
1. with formal argument 2. Without formal argument
Method terminologies: method definition method signature
access modifier method returns types[ method name (formal argument)]
{
Method block/method body/method implementation
}

1. With return type


2. Without formal argument

1. Without return type


2. With formal

1. With return type


2. With out formal argument

1. With return type


2. With formal argument
Note: we cannot write one method inside another method that means nested method not possible in java .We can call
a method n number of times.
Class A
{
Public static void main(string)
{
Public static void add () {
s.o.p. ( ) this is not possible in java
}
}
 We can write any number of methods inside a class
 We can call a method” n” number of types
1. without return type & without formal argument
In this method we won’t have any type of return type in java we indicate that as ‘’void’’ void is key words it should be
always return lower case
EX: calss A {
Public static void main ( string [])
{
A. add( ) ;
}
public static void add ( )
{
Method call:
We can call a static method in two ways without object creation those are
I. Directly without class
II. With class
Without class name: a method is called using the method signature of the particular method and it should end with a
semicolon (;)
EX: add (); n
With calls name: a method class
With class hear we prefix class name then we use (.) operators then we continue with method signature ending with(;)
Ex: A.add ();
Ex: class A {
Public static void main (string []args){
s.o.p (‘’start”);
A. add ();
s.o.p(‘’end”)
public static void add( )
{
Int a =10;
Int b = 20;
Int = ( a+ b)
s.o.p( c);
}
}
Create a class operation it have two method name add & subtract and inside main method call both the methods
EX: class A {
Public static void main (string [ ] args ){
s.o.p (‘’start’’);
A. add ( ) ;
s.o.p(‘’end”)
public static void add ()
{
Int a =10;
Int b = 20;
Int = (a+ b)
s.o.p(c);
}
}
Method chaining: One method calling another method is known as method change
EX: class A {
Public static void m1 () {
S.o.pln(m1);
A.m2 ();
}
Public static void m2 () {
s.o.pl(m2);
}
Public static void main (String [] args) repeats the out put
{
A.m1( ) ;
}
Stack over flow error:
Ex: class A{
Public static void m1( ) 
{
s.o.pln(m1);
A.m2( );
}
Public static void m2 ( ) {
----------- A.m1( ) ;
S.o.pln(“m2”); out put: m1,m1, : infinity
}
Public static void main ( String[ ] args) run time error
{
A.m1( ) ; 
}}
 It is run time error occur when the stack area is filed with frames
 Whenever method calls another method and that another method calls again the first method then it will be result
in stack over flow error
 If there are “n” number of method there should be always be “n-1” no of calling statement
Method recursion: method calling itself is known as “method recursion”
Ex:
Class A{
Public static void m1( ){
S.o.pln( m1); output: m1, m1,m1: infinity times run
A.m1( ) ;
}
Public static void main ( string [ ] args ){
A.m1( ) ;
}}
Printing “numbers “by using method recursion?
Class A{
Public static void num( ) {
s.o.pln(i);
i++
if(i<=3){
A.num( i); output: 1, 2,3
}}
Public static void main (String [ ] args ) {
A.num(1 )
}}z

Method over loading


In class having two are more methods with same method name but deferent in the formal argument is known as method
over loading
Rules to archive method overloading:
 Number of formal arguments should be deferent
 If the n of formal argument should be same data type must be deferent
 If the data type is same the position of the data type must be deferent
EX: class A {
Public static void m1 (int a double b) {
s.o.pln(1); }
public static void m2( int b double a ){
s.o.pln(2);
}
Public static void main( ) {
A.m1(10,200.00)
A.m1(200.4,30)
}}
EX: over loading main method
Class overload {
Public static void main( String[ ] args){
S.o.pln(“main method” );
Overload.main( 1);
Overload.main(20.0);
Overload.main(“Hi”);
}
Public static void main( int a){
S.o.pln(1);
}
Public static void main(double b ){
s.o.pln(2);
public static void mian(string c) {
S.o.pln(3);
}
}
Crate class name solution and have 3 add methods one with two integer formal argument 2 nd method have 3 integer
3rds with 4 integer formal argument in side call all 3 methods
public class Method_overloading {
public static void add(int a ,int b ) {
System.out.println(a*b);
}
public static void add(int a, int b, int c) {
System.out.println(a+b+c);
}
public static void add(int a , int b, int c,int d) {
System.out.println(a + b+ c+ d);
}
public static void main( String[] args) {
Method_overloading.add(4,5);
Method_overloading.add(10,30,40);
Method_overloading.add(10,30,40,20);
}
}

Variable: in variables we have 2types


 Local variable
 Global variable
Local variable: local variable are return inside class block and inside method block or constructer or multiline initializes
 Local variable once declare it should be initialized or else it will Thore complier time error because local variable
dose not default values
 Whenever local variable and global variable names are same and when we try to fetch it directly always local
variable will be dominate
Calss A{
Public class Demo{
Static int a = 10;
Public static void main (String [ ] args )
Int a = 20;
S.o.pln(a);
}}
Local variable dose not have concept of static and non- static
Global variable: global variable are written in side class block outside any other block
 In global variable we have two types
 Static
 Non- static
Static variable:
 static variable are pre fixed with static keyword and static means only one copy for class
 static variable should access with class name variable name
 whenever there is local variable, we can also call directly with variable name but this should not be only local
variable with the same variable name because if there is a variable with same name that local variable will
dominate over the global variable
Non- static:
 non-static variable are not pre fixed with static keyword and non-static variable are multifuel copays for class
based on the object creation
 even non-static variable has default value
 non-static variable can be access with only object creation
EX:

Calss A { Main area Class area Static area Help area


Static int a =10; Main () f/0
Main(){ class A { {
Int x= 10; Main () ox1 Int x = 20;
S.o.pln( x); { X 0 20
s.o.pln( A.x); Int x = 20; 10
} s.o.pln (x);
}
s.o.pln(A.x); Main () – 0x1 s.o.pln ( x);/20

}
Static variable
s.o.pln ( A.x);/10 Static method
1). main method ()
JVM 2). Others ()

Non static variable:


Class A{
Static int x=10;
Int y= 20;
Public static void main(){
s.o.pln(A.x);//10
A a =new A();
s.o.pln(a.y);//20
}}

Class A{ Main area Class static area Static area Help area
Static int x=10; Main ( ) ox1 A()
Int y= 20; s.o.pln(A.x); Int x 0 10
Public static void Main () ox1
main(){
s.o.pln(A.x);//10
A a =new A();
s.o.pln(a.y);//20
}}

Create class and have 2 static properties and 2 Non- static properties inside main method and call all 4 properties and
showcase the execute process

Constructor: - constructor is special type of method where in class name as constructer name
 constructer are used to initialized object and use to execute line of code (or) set of instructor whenever the object
created and it is mainly used to load initialized and register Non-static properties
 constructor does not have concept of static and non-static
 constructor don’t have “return type”
 if you write a return type it will become “method”
 we can’s write return statement inside a constructer
EX: class A{
A(){
s.o.pln(“object created”);
}
Main( ) {
A a=new A();;
New A();
New A() Abundant object means the object are created but not stored
}}
Calss car {
Int regno;
Private int num=1000; 1. Load
Car (){ 2. Initialize
3. Register
s.o.pln(“car is purchased”);
regno = num;
num++;
Out put //
S.o.pln(“ registered” num+regno);
Car purchased
}
1000
Public static void main( ) {
Car purchased
Car c1=new car();
1001
Car c2=new car ();
}}
MA CSA SA HA

Types of constructors: -
There are 2 types of constructors 1. Default constructor 2. Custom/ user default constructers
1. default constructor:
 the constructor which is added by the compiler implicitly known as “default constructor”
 default constructor does not have any formal arguments (FA)
 the access modifier depends on the class access modifier
2. custom constructer
 the custom constructer which is added by the programmer is known as “custom constructer”
in custom constructor we have 2 types
 parametrized
 non-parametrized
a. parametrized: - constructer added by the programmer explicitly with formal argument is know as “parametrized
custom constructer”
b. non- parametrized: - constructer added by the programmer without any FA is known as “ non-parametrized”
create class employee have 2 static variable 2 non-static variable 2 static methods and 2non-static method and inside
main method call all the properties and showcase execute process ?
class Employee{
static int id=6;
static string name=”mahi”;
long mobile num=9876543211;
int age = 22;
public static void m1(int id){
s.o.pln(6);
}
Public static void m2( string name){
s.o.pln(“mahi”)’
}
Public static void m3(long mobile number){
s.o.pln(9876543211);
}
Public static void m2 ( int age){
s.o.pln(23);
}
Public static void main( String [ ] args ){
S.o.pln( empid);
Employee.m1(int id );
Employee.m2(String name);
Employee e1.new employee ();
E1.m1();
Employee e2.new employee ();
E2.method2();
Constructor:- it is used to initialize the non-static variable
EX: Class Employee{
Int id ;
Employee(int i){
Id = i ;
}
Public static void main( ){
Employee A1=new employee(1);
S.o.pln(A1.id);
Employee A2=new employee(2);
S.o.pln(A2.id);
}}
Create class student and have 3 non-static variable names, id, age initialize this which the help of custom constructer
inside main method create 3 student object and with each reference call all the properties
Class Student {
String name;
Int id;
Int age ;
Employe( string n, int n1, int n3)
name=n;
id=n1;
age=n3;

public static void main(){


employee m=new employee (“mahi”,);
s.o.pln(m.name);
employee m1=new employee (1);
s.o.pln(m1.id);
employee m2=new employee (24);
S.o.pln(m2.age);
}}
Display method :-
Public class Student {
Int id;
String name;
Int age;
Student (int I, string n, int a,){
Id =I;
Name=n;
Age =a;
Void display( ) {
S.o.pln(“student id is” +id+ “student name is “ +name+” student age is”+age);
}
Public static vid main( ) {
Student s1 = new student (1,”mahi”,24);
Student s2 =new student (2,”pramodhu”, 22);
Student s3 = new student ( 3, “Gokul”, 24);
S1.display();
S2.display( ) :
S3.display();
This.
 This is keyword: -this is key word present in java since it is a keyword is should be writer lower case
 This keyword used to refer current class object
 Whenever global variable and local variable name is same to differentiate b/w them we make use of this keyword
 This keyword is refixed with variable name followed with . (dot)
Create class mobile and have 4 non-static variable name brands, cost, color, ram, initialize with help of custom construer
using this key word and used display method and print and inside main method create 4 object & call all the properties
Calss mobile{
String brand name;
Int cost;
String ram;
String colors;
Mobile (string brand name, int cost, int ram, string colors) {
This .brandname= brandname;
This.cost=cost;
This ram=ram;
This.colors = colors;
}
Void display (){
s.o.pln(brandname+ “ “ +cost+ “ ” + ram+ “ ” +colors)
}
Public static void main(){
Mobile a1= new mobile (“apple”,50000,”4gb”,” white”);
Mobile a2=new mobile (“Samsung”, 40000, “6gb”,” black”);
Mobile a3 = new mobile ( “mi” ,39000,”12gb”,” green”);
a1. Display ();
a2. Display();
a3. Display ();
}}
Creae class name cricket player and have four non static variable name, jersey no, age, highest score initializes this with
the help of constructure in main method 11 objects and call the display method for showcase the process
Class Cricket&player {
String name;
Int jersey no;
Int age;
Int highest scores
Cricket&player(string name, int jersey no, int age, int highest scores)
This.name=name;
This.jersey no = jersey no;
This.age =age;
This.highest scores= highest scores;
Void display (){
s.o.pln(name+ “ “ +jersey no+ “ “ +age+ “ “ +highest scores)
}
Main(){
Cricket&player c1=new Cricket&player( “Rohith”,38,37,100);
Cricket&player c2=new Cricket&player( “virat”,18,35,90);
Cricket&player c3=new Cricket&player( “hardick”,33,30,120);
Cricket&player c4=new Cricket&player( “sachin”,01,40,80);
Cricket&player c5=new Cricket&player( “jadeja”,22,34,50);
Cricket&player c6=new Cricket&player( “aswin”,20,33,77);
Cricket&player c7=new Cricket&player( “bumra”,19,35,30);
Cricket&player c8=new Cricket&player( “suresh”,10,34,40);
Cricket&player c9=new Cricket&player( “Karthik”,14,30,39);
Cricket&player c10=new Cricket&player( “dhoni”,12,39,100);
Cricket&player c11=new Cricket&player( “mahi”,34,25,50);
C1.display();
C2.display();
C3.display();
C4.display();
C5.display();
C6.display();
C7.display();
C8.display();
C9.display();
C10.display();
C11.display();
}}

Constructer over loading: - in a class having two are more constructers with deferens in the formal arguments is known as
constructure over loading.
Rules: -
 number of formal arguments should be same
 Number FA should be same data type should be deferent
 Data type should be same position data type should be deferent
EX: class A{
A(){
s.o.plnt( “A() );
}
A( int a) {
s.o.pln( “ A(int a)”) ;
}
Main( ) {
A a= new A();
A a1= new A ( 10);
}}
EX:
EX: public class OUT_put {
int i;
int j;
OUT_put(){
i=10;
}
OUT_put(int n){
j=n;
}
public static void main(String[] args) {
OUT_put a=new OUT_put();
OUT_put a1=new OUT_put(20);
System.out.println(a.i);
System.out.println(a.j);
System.out.println(a1.i);
System.out.println(a1.j);
}
}
EX:
public class Box {
int width;
int lenght;
int depth;
String color;
Box(String color){
this.color=color;
System.out.println("box is create");
}
Box(int n);
width=n;
lenght=n;
depth=n;
System.out.println("box is created");
}
Box(int width, int lenght,int depth, String color){
this.width=width;
this.color=color;
this.depth=depth;
this.lenght=lenght;
}
public static void main(String[]args) {
Box b1=new Box("black");
Box b2=new Box(10);
Box b3 = new Box (10,15,30,"white");
b1.display();
b2.display();
b3.display();
}
void display() {
System.out.println(width+ " "+color+""+depth+""+lenght);
}
}
Constructer chaining: - one constructer calling another constructer is known as constructer chaining, we can archive this
2ways that is using this (); super ();
This ();
 This (); call is use to call the constructer present in the same class
 This (); should be the fast executable line inside constructer
 We can’t write more than one this( ); inside on constructer
Ex: class A {
A() {
this(1); Out put

1
s.o.pln(1);
}
A(int a ) {
s.o.pln(2);
}
Main(){
A a = new A();
}}

Oops
Inheritances: - aquiring property from super calls to sub class with the help of extends key word is known as inheritances
 The class which donates the properties is known as super class
 The class which accepted the property is called as sub class
 Super class is also known as parent class and also is known as base class
 Sub class also known as child class (or) derived class
 Inheritances archive using extends key words
EX:

class A{ Class B extends A {


Int x =10; Int y= 20;
} Main(){
B b=new B ();
s.o.pln( b.y);
s.o.pln(b.x);
}}

Only variable and methods we can inherit


Block and constructers we can not inherit
Create class A vehicle and have two non-static variable and create another class name bike and inherit properties from
vehicle and also have two non- static variable name bike class and in side a main method create object bike and call all
4non- static variable
Class Vehicle{
Int price = 100000;
String brand=”BMw”;
}
Class Bike extends Vehicle {
String color=”blue”;
Int cc = 100;
Public static void main( ) {
Bike b = new Bike ();
S.o.pln(b.color);
s.o.pln(b.cc);
s.o.pln(b.brand);
s.o.pln(b.price);
}}
create class human and have 4 non-static variable create class students and have 4non-satic variable and inherit
properties human and in side main method
public class Human {
String vilagename="kalyandurgam";
String name="mahi";
int age =25;
int pnum=987654321;
}
public class Student extends Human {
String sname="mahi";
String clgname ="swamy vivekananda";
String group="CEC";
int sid=99;

public static void main(String[] args) {


Student s=new Student();
System.out.println(s.clgname);
System.out.println(s.group);
System.out.println(s.sid);
System.out.println(s.sname);
System.out.println("__________________________");
System.out.println(s.vilagename);
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.pnum);
}
}

TYPES OF INHERITANCE
1. Single level inheritance
2. Multi level inheritance
3. Hierarchical inheritance
4. Multiple inheritance
5. Hybrid inheritance

1.Single level inheritance : acquiring properties from single parent class to single child class this known
single level inheritance
Ex:
package org.training; EX:
Mobile class

public class Mobiles {

I phone 16 class
String Mname="Hyryder";
int Mprice = 100000;
void m1() {
System.out.println("hi");
}
void m2() {
System.out.println("hlo");
}
}

class Iphone16 extends Mobiles{

String brand ="apple";


int price =100000;
void m4() {
System.out.println("siri");
}
void m3() {
System.out.println("msg");
}
public static void main(String[] args) {

Iphone16 b =new Iphone16();


System.out.println(b.brand);
System.out.println(b.price);
System.out.println(b.Mname);
System.out.println(b.Mprice);
b.m1();
b.m2();
b.m3();
b.m4();
}

2.Multi level inheritance : combination of super classes and sub classes from top to bottom sequential way is known
as Multi level inheritance
public class A { EX:
Class A
int nprice=100;
String nname="spider man nodes";
void nodes() { Class B

System.out.println("nodes");
} Class C
}
public class B extends A{
int nprice1=200;
String nname1="Bat Man nodes";
void batman() {
System.out.println("Bat ");
}
}
public class C extends B{
int nprice2 = 150;
String nname2= "Iron Man nodes";
void iron() {
System.out.println("Iron");
}
public static void main(String[] args) {

C c1 =new C();
System.out.println(c1.nname);
System.out.println(c1.nprice);
System.out.println(c1.nname1);
System.out.println(c1.nprice1);
System.out.println(c1.nname2);
System.out.println(c1.nprice2);
c1.iron();
c1.batman();
c1.nodes();
}
}

3.Hirachical inheritance : having one parent class two or more child classes is known as hirachical inheritance
public class vechical { EX: parent class
int bcost= 10000; Vehicle class
int cprice=1900000;
void vechicalcondition() {
System.out.println("condition is good and nice"); child class child class
} Bike class Car class
}

public class Bike extends vechical {


String color="green";
int bikecc=1000;
void bikeshowroom() {
System.out.println("kawasaki");
}
public static void main(String []args) {
Bike b=new Bike();
System.out.println(b.bcost);
System.out.println(b.color);
System.out.println(b.bikecc);
b.vechicalcondition();
b.bikeshowroom();
}
}

public class Car extends vechical {


String ccolor="white";
int ccc=2599;
void carshowroom() {
System.out.println("HARSHA TOYOTA");
}
public static void main(String[] args) {
Car c=new Car();
System.out.println(c.cprice);
System.out.println(c.ccolor);
System.out.println(c.ccc);
c.vechicalcondition();
c.carshowroom();
}
}

SUPER KEYWORD : super keyword can be used in two ways


super() :
1.to call global variable super class
2.to call constructor of super class
3. to use super keyword inheritance mandatory
SUPER KEYWORD :
super keyword for variables

syntax :
super.variable name ;
this syntax we will used to call the global variable present in the super class and it can be used non static variable as well
as non private variable
inside static method of static context, we cannot used super key word and this key word
ex :

Class A{ Class B extends A{


Int x=10; Int x = 20;
} Void m1( ){
Class x present in super call Int x=30; Calss x present in same class
s.o.pln(x);
s.o.pln(this.x);
s.o.pln(super.x);
}
Public static void main( String [ ] args){
B b=new B();
b.m1( );

Inside static method or static context we can’t use super keyword (or) this key word.
call to super :
call to super is use to call the super class constructor.

Public class A{ Class B extends A{


A(){ B(){
This(1); this(1); Out put
S.o.pln(“A1”); S.o.pln(“b1”); A2
} } A1
A(int a){ B(int a ){ B2
Super(); B1
S.o.pln(“b2”);
}
Public static void main(String [ ] args){
B b=new B();

This & super keyword we can’t use in static method.


Static method we can call class name (.) dot and method name.
We can’t create object int static method.

Multiple inheritances: multiple inheritance having two or more parents one child class is known as
multiple inheritances parent class parent class
Ex: Class A{ Class B{

Int a = 10; Int a = 10;

} }
Child class
Class A{

Int = 10;

Hybrid inheritances : hybrid inheritances combination of 2 or more types of inheritances is known as


hybrid inheritance
Whenever we conclude multiple inheritances, hybrid not support
Whenever multiple is not include hybrid will support

Create class

Blocks :
Blocks are used to initialize to these blocks are also multi line initialize in a class we cannot declare a variable and we
cannot initialize because it will lead error
Ex: class A{
Int x ;
x = 20; Output: -
main( ) { Compile time error
s.o.pln ( x);
}
To Initialize we have block and in blocks we have two types
1. Static
2. No-static

Static block:
 In static blocks we cannot initialize static variable
 In Static blocks we cannot initialize no-static variable with static key word
 In static blocks we cannot initialize non-static variable
 Static blocks are the one which are pre fixed with static key word
 Static blocks are execute before main method from top to bottom sequentially
 We can write “ n “ number of static blocks as for requirement
 We can create object in side static blocks we can run loops, decision making statement inside the static blocks

Non-static blocks
 Non-static blocks use to initialize non-static variable as will as we can initialize static variables
 This are not prefixed with static keyword
 It will be execute whenever we crate object top to bottom squatly
 Whenever we create object in side non-static blocks it is not compile time error but it will result in “ run time
error”.

Method over Riding:


1. To archive method over riding inheritances in mandatory
2. Inheriting non-static form super class to sub class with same method declaration but changing in method
implementation is known as method over riding
3. Overriding is not possible for static methods, final methods, privet methods.
4. While overriding method declaration should be same
5. Access modifiers should be same or it should have higher visibility
 Modifiers should be same
 Return type should be same
 Method type should be same
 Formal argument should be same
Ex:
Class Animal { Class Dog Class Cat Class Tiger Class Snake Test {
extends Animal extends Animal extends Animal extends Animal Main ( ) {
{ { { { Dog d = new Dog ( );
Void make sound { Void run () { d.makeing_sond();
s.o.pln( “animal is Void Void making Void s.o.pln( “snake d.run ();
making_sound “) making_sound() sound( ) { making_sound( ) scrolling” ) ;
} { s.o.pln( “maw { } Cat c = new Cat ( ) ;
Void run( ){ s.o.pln(“bow maw”) ; s.o.pln(“roow”) Void c.making_sound ();
s.opln(“run”) bow”) } } making_sound () c.run ();
} } } } {
} } s.o.pln( “hiss”); Tiger t = new Tiger();
} t. making_sound
} t.run();

Snake s = new Snake():


s.run();
s.makin g_sound();

@Over ride
WhatsApp version.1 {
Void message {
s.o.pln( “text message “) ;
}
Void calls {
S.o.pln( “no calls” ) ‘
}

WhatsApp version.2 extends WhatsApp version.1 {


@Override
Void message {
s.o.pln( “text message with stickers “) ;
}
@Override
Void calls {
S.o.pln( “audio calls and video call” ) ‘
}

WhatsApp version.3 extends WhatsApp version.2 {


@Override
Void message {
s.o.pln( “text message with stickers videos “) ;
}
@Override
Void calls {
S.o.pln( “audio calls video call and group call” ) ;
}

Test extends WhatsApp version.3{


Main( ) {
WhatsApp version.3 w = new WhatsApp version.3( );

Access modifier :-
 @Override it was interduces from JVM 1.5 it is used with override method we write @Override before
writing over method
EX:
@Override
Void m1(){
S.o.pln();
}
 When we write @Override to sub class method it will throw error if the same method is not present in
super class
 While we override programmer can do mistakes while writing the sub class method to identify this we
use @override

No-non primitive Type costing:-


it as two types
1. Upcasting
2. Down casting
 Up casting:- creating a instances of sub class and assigning the references to super class type
variable is called as up casting
 Up casting is implicitly process
 Inn up casting we can only access supper class properties not sub class properties
 When sub class instances is up casting using super class type references variable sub class specific
cannot be refer
EX:- A a = (A) new B ();
Class A { Class B extends A {
Int x= 10; Int y =20;
} }
Out put :-
10//
20// compile time error
Main ( ) {
A a = (A) new B();
s.o.pln( a.x);
s.o.pln ( a.y);
}
}

down casting :
Ex:
Class A{ Class B extends A{
Int x =10; Int y=20;
Main( String[] args)
} {
A a = new B();
B b= (B) new a;
S.o.pln (b.x);
s.o.pln (b.y);
}
}
Down casting: -
 Costing super type references to sub type is known as down casting.
 Using super type references sub class sub type specify properties can’t be referee to refer sub type
specific properties
 Super type references should be down casted to subtype
 Down costing is explicitly to archive down casting up casting is mandatory
 To archive up casting inheritances in mandatory
 With up casting s we can only refer super class properties
 With down coating we access both super class and sub class properties

Calss cast exception:-


 Whenever we hub cast to one class and try to down cast to another class I will result in class cast
exception
Ex: -
Class A{ Calss B extends Class C extends A {
Int x =10; A{ Int z = 30;
} Int y = 20; }
} Public static void main ( String []args)
{

A a = new B ( ) ;
B b = ( b) a;

Instanceof operators : -
 instances of operators check whether the object belong to the specific class type or not .
Syntax : - reafferences instanceof type/class name
a instances of B.
 It return “true” if the references variable is instances of the given type of run time
Ex:
Class A{ Calss B extends A{ Class C extends A {
Int x =10; Int y = 20; Int z = 30;
} } }
Public static void main ( String []args)
{

A a = new A( );
B b = new B( );
C c = new C( );
S.o.pln ( a instance of A); // true
S.o.pln ( b instance of B); // true
S.o.pln ( c instance of C); // true
Class cast exception
Calss B extends A{ Class C extends A {
Class A{ Int y = 20; Int z = 30;
Int x =10; } }
} Public static void main ( String []args)
{
A a = new B();
If ( a instancesof B ){
B b = (B) a;
s.o.pln( 1);
if ( a instancesof C){ output:-- 1
C c = (C) a ;
s.o.pln(2);

You might also like