Professional Documents
Culture Documents
Principles of OOP
Principles of OOP
Principles of OOP
• Abstraction: Abstraction means using simple things to represent complexity. We all know how to
turn the TV on, but we don’t need to know how it works in order to enjoy it.
• Encapsulation: This is the practice of keeping fields within a class private, then providing access
to them via public methods. It’s a protective barrier that keeps the data and code safe within the class
itself. This way, we can re-use objects like code components or variables without allowing open access to
the data system-wide.
• Polymorphism : This Java OOPs concept lets programmers use the same word to mean
different things in different contexts. One form of polymorphism in Java is method overloading. That’s
when different meanings are implied by the code itself. The other form is method overriding.
1. Inheritance
• The idea behind inheritance in java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class,
you can reuse methods and fields of parent class, and you can add new
methods and fields also.
subclass: Employee
• The subclass can: - employeeID: int
• Add new functionality - salary: int
• Use inherited functionality - startDate: Date
• Override inherited functionality
Inheritance
• When an object is created using new, the system must allocate
enough memory to hold all its instance variables.
• This includes any inherited instance variables
• In this example, we can say that an Employee "is a kind of" Person.
• An Employee object inherits all of the attributes, methods and
associations of Person
Person
- name: String Person
- dob: Date name = “abc"
dob = Jan 13, 1954
Employee
is a kind of name = “xyz"
Employee dob = Mar 15, 1968
- employeeID: int employeeID = 37518
- salary: int salary = 65000
- startDate: Date startDate = Dec 15,
2000
Inheritance Hierarchy
• Each Java class has one (and only one) superclass.
• C++ allows for multiple inheritance
Class
Accessing function
class ParentClass {
package test;
class ParentClass {
int var=10;
}
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
class Animal{
void eat(){ class Cat extends Animal{
System.out.println("eating...");} void meow(){
} System.out.println("meowing...");}
class Dog extends Animal{ }
void bark(){ class TestInheritance3{
System.out.println("barking...");} public static void main(String args[]){
} Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//???
}}
meowing...
eating...
Super Keyword
Super Keyword
• super is a keyword.
• It is used inside a sub-class method definition to call a method defined in
the super class.
• The super keyword refers to the objects of immediate parent class.
• Private methods of the super-class cannot be called.
• Only public and protected methods can be called by the super keyword.
• It is also used by class constructors to invoke constructors of its parent
class.
• To access the data members of parent class when both parent and child
class have member with same name
• To access the method of parent class when child class has overridden that
method.
super keyword to access the variables of parent class
class Superclass
{
int num = 100;
}
void printNumber(){
System.out.println(num);
}
class Superclass
{
int num = 100;
}
class Subclass extends Superclass
{
int num = 110;
void printNumber(){
System.out.println(super.num);
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printNumber();
}
}
super keyword to access the function of parent class
class ParentClass {
class Parentclass
{
void display(){
System.out.println("Parent class method");
}
}
class Subclass extends Parentclass
{
void printMsg(){
display();
}
public static void main(String args[]){
class ParentClass {
ParentClass() {
System.out.println("Parent class constructor ");
}}
}}
Teacher
public class Teacher extends Person {
}
Student
int id;
String name;
students[] studentArray = new students[2];
scanner.nextLine();
Student
System.out.print("Student Id:"+studentArray[i].getId());
System.out.print("Student Name:"+studentArray[i].getName());
}}}
Access modifiers
Access modifiers
• Access modifiers are those which are applied before data members or methods of a class.
These are used to where to access and where not to access the data members or methods.
• Note: Default is not a keyword (like public, private, protected are keyword)
• If we are not using private, protected and public keywords, then JVM is by default taking as
default access modifiers.
Person
class testModifiers {
public int a=20;
Output
• 20
• Hello java
Output ???
class testModifiers {
private int a=20;
private void show(){
System.out.println("Hello java");}
}
class demoModifiers{
• The tightly encapsulated class is a class whose every variable is declared private, whether
getter or setter methods are present or not or they are marked as public or not.
class P
{
int p =19;
//not tightly encapsulated class
}
class Q extends P
{
private int q=20;
//not tightly encapsulated class because it extends class P
}
class R extends Q
{
private int r=23;
//not tightly encapsulated class because it extends class Q
}
Final
Final
• This modifier is used to restrict the further modification of a variable or a method or a class.
The value of a variable which is declared as final can’t be modified once it gets a value. A final
method can not be overridden in the sub class and you can not create a sub class to a final
class.
Without Final Keyword
class testModifiers
{
int a=20;
}
class testModifiers
{
int a=20;
}
class testModifiers
{
final int a=20;
}
}
}
Final keyword with class
class ParentClass {
public void fun() {
System.out.println("Parent class fun ");
}}
class ParentClass {
final void fun() {
System.out.println("Parent class fun ");
}}
• ArrayList is one of the List implementations built a top an array, which is able to dynamically
grow and shrink as you add/remove elements.
• To create an ArrayList, we can simply call the constructor from JAVA’s ArrayList class.
• Here is an example of creating an ArrayList and storing it in a variable so that we can use it:
– ArrayList myList;
– myList = new ArrayList();
• It is as if the ArrayList is a kind of “bag” into which we can place any objects.
• The ArrayList keeps the objects altogether so that we can pass the list around now as a single
object
• just like we can consider a “bag” to be asingle object, although it may contain many other
objects.
ArrayList vs. array
• Construction
– String[] names = new String[5];
– ArrayList<String> list = new ArrayList<String>();
• Storing a value
– names[0] = “x";
– list.add(“x");
• Retrieving a value
– String s = names[0];
– String s = list.get(0);
General format for an ArrayList
Raw ArrayList
• This is the general format for an ArrayList that can hold any kinds of objects.
• However, it is "highly recommended" that we specify the type of objects that will be stored in
the ArrayList.
• We do this by specifying the type between < > characters just before the round
brackets ( ) as follows:
• When constructing an ArrayList, you must specify the type of elements it will
contain between < >.
String student_name;
this.roll_number = roll_number;
this.student_name = student_name;
this.age = age;
}
Student Arraylist Example
import java.util.*;
al.add(s1);
al.add(s2);
al.add(s3);
for(int i=0;i<al.size();i++) {
System.out.print(al.get(i).roll_number+" ");
System.out.print(al.get(i).student_name+" ");
System.out.println(al.get(i).age);
} }}