0% found this document useful (0 votes)
20 views6 pages

Javatext

The document provides an overview of key concepts in Object-Oriented Programming (OOP) such as encapsulation, inheritance, polymorphism, method overloading, method overriding, type casting, abstraction, and interfaces. It explains the characteristics of Java classes, memory allocation, and the differences between arrays and collections, as well as the use of generics in Java. Additionally, it discusses the immutability of strings and introduces the collection framework in Java.

Uploaded by

Ekta Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Javatext

The document provides an overview of key concepts in Object-Oriented Programming (OOP) such as encapsulation, inheritance, polymorphism, method overloading, method overriding, type casting, abstraction, and interfaces. It explains the characteristics of Java classes, memory allocation, and the differences between arrays and collections, as well as the use of generics in Java. Additionally, it discusses the immutability of strings and introduces the collection framework in Java.

Uploaded by

Ekta Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

OOPS:

Encapsulation : Binding the data members and related operation into the class is
known as encapsulation.
Encapsulation is defined as the wrapping up of the data and methods under a single
unit .it also implements data hiding.
The encapsulation specifies that data and its related operation must be in the
class body.
It also specifies that the data members should be protected using access
specifiers.
The characteristics of java bean class :1)the class must be public.
2)Private variables
3)Public constructors
4)public getters and setters

Inheritance: one class acquiring the properties of another class.


types of inheritance: 1)single :b->a
2)multiple :not possible in java
3)multi-level :c->b and b->a
4)hybrid : combination
5)hierarchical :b->a and c->a

Every class in java must have a super class, either user defined or compiler
defined.
If a class does not have user defined super class the compiler provide a default
super class by name Object.
Benefits: Code reusability
Easier Maintenance
Faster development
Polymorphism
Encapsulation
Readability

Multiple Inheritance: [A] [B]


extends
[C]

Implicit Constructor call : Whenever we write inheritance program the subclass


constructor must call the super class constructor to initialize the attributes.

If the super class contains non-parameter constructor, the sub class constructor
makes implicit call.

Explicit Constructor call : The subclass constructor can make a call to super class
constructor by using (super ()) super calling statement.
If the super class contains parameterized constructor, the sub class should make
explicit call.(using super())

Constructor calling statement : Super() This makes a call to the super class
constructor it can call with parameter or without parameter.

Rules: 1)Super calling statement be used only inside the constructor body.
2)Super calling statement (super()) must be the first statement in the
constructor body .
3)In one constructor body, only one super calling statement is allowed.

Method Overloading: A class defining multiple method with same name but different
parameter list.
The parameter list should differ in any one of the
following :
1)parameter type
2)parameter length
3)parameter sequence
-In one class we can overload both static and non-
static methods.
-We can overload in the subclass with inherited
methods.
-Whenever we want to perform same operation on
different types of data or set of data we should apply method
overloading.
[Note]: We can overload main method, but the execution begins
only if the main method (String[] args) String array parameter.
The method overloading is used to achieve compile
time polymorphism.

Method Overriding: Inheriting method from super class and changing its
implementation in sub class according to sub class specification.
When a method inherits a superclass method with same
declaration its is called override.
It is used to change or modify the method body.

Type Casting: Casting one type of information to another type is known as


typecasting.
- The type casting is divided into two types:
1)Primitive type casting (data- type casting)
2)Non Primitive type casting(class- type casting)
Primitive type casting : Converting one primitive type of datatype into another
primitive type of data is primitive type casting.
- eg. int x - (int)95.65; // narrowing
- Also known as data- type casting
- Further classified as:
1)Widening - Casting lower data type into
higher data type
- chance of data loss
--double b = (double)54;

2)Narrowing - Casting higher data type into


lower data type
-eg. int a = (int)97.98;
Non Primitive type casting : Casting one class type to another class type
-types: 1) Upcasting
2) Downcasting
-Can be done only for the classes which has Is-
a-relationship.

Upcasting -casting subclass type to super class type


-reference variable must be of super class
-object must be of sub class
- can be done automatically or explicitly
-eg. Father f1 = new Son();
-when upcasting happens the subclass object behaves like
superclass
-subclass properties are hidden

Downcasting -casting superclass type back to sub class type


-reference variable must be of sub class
-object class must ne of super class
-explicitly only
-for downcasting upcasting is compulsory
-eg. Son s1 = (Son) f1;
-downcasting should be performed only on the object which is
upcasted or else we get
"ClassCastException".

Generalization : -Defining a function/method which operates on different types of


object is known generalization.
Eg: Person
-age
-gender
-name
void details(){
// }

Student extends Person Employee extends Person

-age -empid
-gender -sal
-name -designation
@override @override
void details(){// } void
details(){ // }

Government --- void adharcard (Person p) <---- Generalization


void Scholarship(Student s) <-----
Specialization
void TaxPayment(Employee e) <-----
Specialization

Polymorphism: Ability of method to behave differently ,when different objects are


acting upon it.
-An Object showing different behavior at different stages
of life cycle
-Two types of polymorphism :
-Compile Time Polymorphism
-Run Time Polymorphism
Memory Allocation : The JVM uses 4 memory area during execution
1.Heap area
2.Class area
3.Method area
4.Stack area
Heap area: Heap area is used to store objects.
-Non-Static members of class are loaded in heap memory.
Class area: The class area is used to store the static members of the class
-For each class created in project a area is reserved in the
class area and that area is known as static pool.
Method : Stores method body
Stack area : When method is called the method enters the stack memory executes the
statement defined in method body, after finishing of
execution it exits from the stack area
Method Binding : Associating the method declaration to its method body.
Compile time polymorphism : Achieved with the help of method overloading.
-Also known as early binding or Static
binding.
-Method Binding is happening in the
compile time.
-Once binding is done by compiler it
cannot be re-binded.
-Method Overloading is an example of
compile time polymorphism.

Run Time Polymorphism : Achieved with the help of


1)Is-a-relationship
2)Method Overloading
3)Upcasting
-When we call Overridden method on a super
class reference, the methos implementation which gets executed is
dependent on subclass acting upon it.
Eg. Animal a1 = new Cat();
-Out of so many overridden methods, which
method implementation should get executed is decided by JVM at
runtime.
-Also known as Late-Binding/Dynamic-Binding.

Abstraction : Hiding the internal implementation logic of an Object and showing


only the object functionalities.
-The abstraction specifies that don't show the internal
implementation-logic and show only object functionalities.
-Abstract is keyword which is used with class and methods only
-A class which is not used with abstract keyword is called as
concrete class
-Concrete class allows inky concrete method
-A class which declared using abstract keyword is known as
abstract class
-abstract class will allow both abstract and concrete method
-concrete class will not allow abstract method
-concrete method will have both method declarations and method
body
-abstract method will have only declaration part but no method
body
-all abstract methods should be declared using abstract keyword

Rules of abstract :
Rule 1 : When a class inherits an abstract class, we have to
override all the abstract methods and give body
Rule 2:When a class inherits an abstract class, and we don't want
to override the inherited abstract method, then make the subclass as abstract
class.

Interface: Interface is a java type definition which has to be declared using


"interface" keyword.
- Interface is medium between two systems, where one system is
client/user and and another system is object with services
- syntax:
interface InterfaceName{ // }
-Interface can have variable, these variables are automatically public,
static and final
-Interface can achieve Is-a-relationship with class using
implementation keyword
-Interface can allow only abstract methods, those methods are
automatically public and abstract
-When a claass implements ann interface, mandatorily we should override
the abstract methods
-while overriding the method access specifiers should be same or of
higher visibilty
- eg: public ----- to public is correct
public ----- to protected, default, private is wrong
-A class can implement any number of interfaces

IMP: final variable cannot be re-initialize


-final class cannot be inherited
-final method cannot be overridden

Object Class: 1) toString() method


2) equals(): equals() generally compares the address or
reference of two object, In order to compare the data of two object we
have to Override equals()
Rules of overriding equals method
1) Upcasting
2) Downcasting
3) Comparison logic

3) hashCode(): hashCode() return a unique random number for an


Object
hashCode() is used to identify an Object
uniquely
Syntax: public int hashCode(){
return 0;
}
String class : String is predefined final class present in java.lang package.
-String class cannot be inherited because string is a final
class.
-String Objects are immutable in nature.
-String is class as well as a datatype therefore we refer String
as non primitive datatype.
-Default value of String is null.
-String can also be referred as set of char enclosed within
double quotes.
-String object can be created in two different ways :
1)without new Operator (also called as "String literal")
eg: String s = "Abhishek";
2)With new Operator
eg: String s1 = new Strin("EKTA");

-String class also implicitly inherits the object class and has
overridden three methods.
1. toString()
2.hashcode()
3.equals()
1.toString- should have return string representation of the object(address) but in
string class it is overridden to return the data
2.hashcode - should have return a unique address or references of two objects but
in string class it is overridden to compare the data or content.
3.equals - should have compared the addresses or references of two objects but in
string class it is overridden to compare the data or content

Explain how string is immutable in nature?


When we reinitialize a string object , the existing object is not modified.
Rather than that a new object gets created
The reference variable pointing to the old object will get dereferenced and starts
pointing to the newly created object therefore string is immutable inn nature.
the mutable version of string are:
1.StringBuffer
2.StringBuilder

Collection Framework:
->Data Structure : It is a way of storing /Arranging / Organizing the data.
- In other ways DS is arrangement of group of data
-Eg : Arrays ,Linked List
->Collection : Collection is predefined interface present in java.util package
- It was introduced from JDK 1.2
-It is used to store group of data
->Difference between array and collection
Array : Homogenous in nature.
- Fixed Size
- No predefined methods
- Generics not supported
- No underlined data
Collection : Both homogenous and heterogeneous in nature
- Dynamic size
- Many predefined methods available
- Generics supported
- Underlined data structured

Generics : It is a feature used to mention Element type


- It is represented as <E> element type or <T> type
- Using generic it is possible to make a collection of Homogenous as
well as Heterogenous

You might also like